googlemock的ON_CALL与SetArgPointee在测试用例之间不会改变。

huangapple go评论83阅读模式
英文:

googlemock ON_CALL with SetArgPointee will not change from test case to test case

问题

我有一个名为TEST_F的测试,其中包含以下内容:

    struct _pulse thePulse;
    thePulse.code = _PULSE_CODE_DISCONNECT;

    ON_CALL(Mock::Mock_QnxInterface::MockQnxInterface::getInstance(), MsgReceivePulse(_, _, _, _)).WillByDefault(DoAll(SetArgPointee<1>(thePulse), Return(EOK)));

然后,我有另一个测试用例,其中包含相同的代码行,但这次thePulse.code的值是_PULSE_CODE_MINAVAIL

当我运行测试套件时,检查这些常量的case语句总是检测到_PULSE_CODE_DISCONNECT,但从不检测其他情况。

        if (EOK !=  MsgReceivePulse(attach->chid, (void*)&pulse, sizeof(pulse), NULL)) {
             // 这里是错误处理代码
   
            } else {

                switch (pulse.code) {

                case _PULSE_CODE_DISCONNECT:
                    // 清理每个客户端信息的代码
                    break;
                case _PULSE_CODE_MINAVAIL:
                    // 关闭线程       
                    break;
                }
            }

就像模拟函数MsgReceivePulse的返回副作用从一个测试用例传递到另一个测试用例一样。我在这里做错了什么?

我尝试过类似的选项,使用SetArgPointee但没有成功。

英文:

I have a TEST_F with the following:

    struct _pulse thePulse;
    thePulse.code = _PULSE_CODE_DISCONNECT;

    ON_CALL(Mock::Mock_QnxInterface::MockQnxInterface::getInstance(), MsgReceivePulse(_, _,_, _)).WillByDefault(DoAll(SetArgPointee<1>(thePulse),Return(EOK)));

I then have another test case that has the same lines of code, but this time, the value of thePulse.codeis _PULSE_CODE_MINAVAIL.

When I run the test suite, the case statement that checks for those constants always detects _PULSE_CODE_DISCONNECT but never the other case.

        if (EOK !=  MsgReceivePulse(attach->chid, (void*)&pulse, sizeof(pulse), NULL)) {
             // error handling code here
   
            } else {

                switch (pulse.code) {

                case _PULSE_CODE_DISCONNECT:
                    // code to clean up per-client info
                    break;
                case _PULSE_CODE_MINAVAIL:
                    // shut down the thread       
                    break;
                }
            }

It is as though the return side-effect of the mocked function MsgReceivePulse is using the same value from test case to test case. What am I doing wrong here?

I have tried EXPECT_CALL with similar options, using SetArgPointee without success.

答案1

得分: 0

我解决了这个问题。EXPECT_CALL 被正确设置了,然而,因为我正在测试一个多线程的应用程序,我意识到在第一个测试用例之后没有正确停止和重新启动线程。该分支从未被触发,因为那部分代码从未被执行。在进行了更正之后,测试以不同的期望值运行。

英文:

I solved the problem. The EXPECT_CALL was set up properly, however, because I was testing a multi-threaded application, I realized that I was not properly stopping and restarting the thread after the first test case. The branch was never reached because that portion of code was never executed. After making the correction, the tests ran with different expectations.

huangapple
  • 本文由 发表于 2023年7月13日 20:34:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定