英文:
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.code
is _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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论