Search code examples
unit-testinggoginkgogomega

Can Gomega's Equal() handle multiple values ?


I am testing a function to send a mock request to a server using gomega and I want to verify that 1. the request has started 2. the request has completed. For this I am returning two booleans. They should both evaluate to true in the example below but the values will be subject to change. I have tried this:

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))

But it seems that gomega's Equal() does not handle multiple variables. Is there any way around this? Is it bad practice to evaluate two return values ?


Solution

  • You can use a wrapper function I wrote based on your pseudocode

    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}))
    

    I'm using a map instead of simple r1&r2for verbosity, so, you can see what is actually wrong with the results. In my opinion, it's a bad practice to compare 2 return values unless the second one is an error. You can always combine multiple return values in a single language construct (map, slice, struct, etc.) as I did in my wrapper function. I understand it's hard to do with the async Eventually method, but in general, I'd try to assert each of the return values separately.