Gomega 的 Equal() 可以处理多个值吗?

Can Gomega's Equal() handle multiple values ?

我正在测试使用 gomega 向服务器发送模拟请求的功能,我想验证 1. 请求已开始 2. 请求已完成。为此,我 returning 了两个布尔值。在下面的示例中,它们都应评估为真,但值可能会发生变化。我试过这个:

g := gomega.NewGomegaWithT(t)
...
g.Eventually(func() (bool, bool) {
...
    start = false
    end = true

    if (request.status == "started") {
        start = true
    }
    if (request.status == "complete") {
        end = true
    }
    return start, end
}).Should(Equal((true, true))

不过gomega的Equal()好像没有处理多变量。有没有办法解决?评估两个 return 值是不好的做法吗?

gomega's doc中,据说可以使用多个return。

The function that you pass to Eventually can have more than one return value. In that case, Eventually passes the first return value to the matcher and asserts that all other return values are nil or zero-valued. This allows you to use Eventually with functions that return a value and an error – a common pattern in Go

在你的代码中,你能改变这一行吗

.Should(Equal((true, true)).Should(Equal(true, true)

这应该可以解决问题。

编辑:

我忽略了,Equal只接收一个接口参数。我的错。

为了将来参考,比较 gomega 的 eventuallyArray 结构(或任何数据类型)中的多个值可能很有用。

你可以使用我根据你的伪代码编写的包装函数

g := gomega.NewGomegaWithT(t)
...
testedFunc := func() (bool, bool) {
...
    start = false
    end = true

    if (request.status == "started") {
        start = true
    }
    if (request.status == "complete") {
        end = true
    }
    return start, end
}

g.Eventually(func() map[string]bool{
    r1,r2 := testedFunc()
    return map[string]bool{"start": r1, "end": r2}
}).Should(Equal(map[string]bool{"start": true, "end": true}))

我使用的是地图而不是简单的 r1&r2为了冗长,所以您可以看到结果实际上有什么问题。在我看来,比较 2 个 return 值是一种不好的做法,除非第二个值是错误的。您始终可以将多个 return 值组合在一个语言结构(映射、切片、结构等)中,就像我在包装函数中所做的那样。我知道很难使用 async Eventually 方法,但总的来说,我会尝试分别断言每个 return 值。