Google 模拟,预计 2 个给定号码之间的通话

Google Mock, expect call between 2 given numbers

我正在使用 google 模拟作为测试实时系统的框架,我想验证传感器的配置是否正确实施。为此,我向所述传感器发送了一个新频率,并期望我的传感器数据回调被调用 x 次。

因此我必须让测试线程休眠几秒钟,所以我的回调可以被调用几次。

因此预期的调用次数不是固定的,例如,如果我希望我的回调每秒被调用一次并且我让线程休眠 5 秒,它实际上可以被调用 4 到 6 次,否则测试会太严格。

这就是问题所在,我还没有找到一种方法来测试 expect call 是否在 4 到 6 之间,我尝试了以下方法:

EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AnyNumber());
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtMost(6));
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtLeast(4));

EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AnyNumber());
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtLeast(4));
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtMost(6));

https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#cardinalities-cardinalitylist 开始尝试 Between。正是为了断言给定调用在 mn 次之间被调用。