dsfsdf5646 2018-04-14 07:01
浏览 138
已采纳

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

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 ?

  • 写回答

2条回答 默认 最新

  • doukong9982 2018-04-14 11:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)