如果预期调用过度饱和,Google 测试/模拟测试会失败
如果谷歌模拟方法被调用的次数超过预期次数,如何使谷歌测试失败?
这是示例:
class MockIO : iIO
{
MOCK_METHOD1(IO_Read, void (uint8_t));
};
TEST(case, test)
{
MockIO io;
EXPECT_CALL(io, IO_Read(0x01)).Times(10);
for (i=0; i<20; i++)
io.IO_Read(0x01);
}
据我了解,我应该尝试
EXPECT_CALL(io, IO_Read(0x01)).Times(10).Throw(exception);
但在嵌入式项目中未使用异常。
有什么想法吗?
How can I make google test fail if google mocked method is called more than expected times?
Here is the example:
class MockIO : iIO
{
MOCK_METHOD1(IO_Read, void (uint8_t));
};
TEST(case, test)
{
MockIO io;
EXPECT_CALL(io, IO_Read(0x01)).Times(10);
for (i=0; i<20; i++)
io.IO_Read(0x01);
}
As I understand I should try
EXPECT_CALL(io, IO_Read(0x01)).Times(10).Throw(exception);
But in embedded projects exceptions not used.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该在测试中使用严格的模拟。
使用 Strictmock 时,非预期调用会导致测试失败。
http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks
I think you should use strict mocks for your test.
When using a Strictmock, not expected calls cause a testfailure.
http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks
没关系,因为您不应该构建在嵌入式平台上运行的单元测试,而应该为您的 PC 构建单元测试。
设置期望可以简化为:
未能满足条件将引发异常。
要使 googlemock 库能够在预期失败时抛出异常:
It doesn't matter, because you should not build your unit test to be run on the embedded platform, but for your PC.
Setting the expectations can be reduced to this :
Failing to satisfy the condition is going to throw an exception.
To enable googlemock library to throw exceptions on failed expectations :