Gomega的Equal()能否处理多返回值?请求启停测试场景咨询
Gomega多返回值断言的用法
好问题!Gomega完全支持对多返回值直接使用Equal()断言,刚好能匹配你验证请求启动和完成状态的需求。
你可以直接让Eventually的匿名函数返回两个布尔值,然后在Should()里用Equal()传入对应的预期元组,Gomega会自动逐个对比每个返回值是否符合预期。
结合你的代码场景,完整的写法应该是这样:
g := gomega.NewGomegaWithT(t) // 假设request是你的模拟请求实例 g.Eventually(func() (bool, bool) { start := false end := false if request.Status == "started" { start = true } if request.Status == "complete" { end = true } return start, end }).Should(gomega.Equal((true, true)))
额外说明:
- 灵活匹配不同状态:如果需要分步验证(比如先确认请求启动,再确认完成),可以拆分出两个独立的断言:
// 第一步:验证请求已启动但未完成 g.Eventually(func() (bool, bool) { start := request.Status == "started" end := request.Status == "complete" return start, end }).Should(gomega.Equal((true, false))) // 第二步:验证请求最终完成 g.Eventually(func() (bool, bool) { start := request.Status == "started" end := request.Status == "complete" return start, end }).Should(gomega.Equal((true, true)))
- 类型一致性要求:只要你的匿名函数返回值的类型、数量和
Equal()传入的预期元组完全一致,Gomega就能正确解构对比,不用担心兼容性问题。
内容的提问来源于stack exchange,提问作者user2969402
相关产品推荐
相关产品推荐

