谷歌嘲笑——至少是众多期望中的一个
Google mock - at least one of multiple expectations
我使用Google Mock为外部API指定一个兼容层。在外部API中,执行某些操作有多种方法,因此我希望指定至少满足一组期望中的一个(或者最好是一个)期望。在伪代码中,我想这样做:
Expectation e1 = EXPECT_CALL(API, doFoo(...));
Expectation e2 = EXPECT_CALL(API, doFooAndBar(...));
EXPECT_ONE_OF(e1, e2);
wrapper.foo(...);
这是可能做到使用谷歌模拟?
有两种方法:
- 带有自定义方法的执行器-创建一个类,使用相同的方法。然后使用
Invoke()
将调用传递给该对象 - 使用偏序调用-在不同的序列中创建不同的期望,如这里所述
带有自定义方法executor
像这样:
struct MethodsTracker {
void doFoo( int ) {
++ n;
}
void doFooAndBar( int, int ) {
++ n;
}
int n;
};
TEST_F( MyTest, CheckItInvokesAtLeastOne ) {
MethodsTracker tracker;
Api obj( mock );
EXPECT_CALL( mock, doFoo(_) ).Times( AnyNumber() ).WillByDefault( Invoke( &tracker, &MethodsTracker::doFoo ) );
EXPECT_CALL( mock, doFooAndBar(_,_) ).Times( AnyNumber() ).WillByDefault( Invoke( &tracker, &MethodsTracker::doFooAndBar ) );
obj.executeCall();
// at least one
EXPECT_GE( tracker.n, 1 );
}
使用偏序调用
TEST_F( MyTest, CheckItInvokesAtLeastOne ) {
MethodsTracker tracker;
Api obj( mock );
Sequence s1, s2, s3, s4;
EXPECT_CALL(mock, doFoo(_)).InSequence(s1).Times(AtLeast(1));
EXPECT_CALL(mock, doFooAndBar(_,_)).InSequence(s1).Times(AtLeast(0));
EXPECT_CALL(mock, doFoo(_)).InSequence(s2).Times(AtLeast(0));
EXPECT_CALL(mock, doFooAndBar(_,_)).InSequence(s2).Times(AtLeast(1));
EXPECT_CALL(mock, doFooAndBar(_,_)).InSequence(s3).Times(AtLeast(0));
EXPECT_CALL(mock, doFoo(_)).InSequence(s3).Times(AtLeast(1));
EXPECT_CALL(mock, doFooAndBar(_,_)).InSequence(s4).Times(AtLeast(1));
EXPECT_CALL(mock, doFoo(_)).InSequence(s4).Times(AtLeast(0));
obj.executeCall();
}
相关文章:
- C++:递归地将字符串中一个字母的所有实例替换为另一个字母
- C++中一个函数对数组进行搅乱和平方运算的问题
- 将文本流中一个字符串的所有匹配项替换为另一个字符串
- C++中一个非常简单的枚举类错误
- 有没有更好的方法来检测向量中一个项目的多次出现?
- 这是 c++ 中一个好的 goto 语句吗?
- 如何使用C/C 系统调用来获取Linux过程中一个过程的当前大小
- 如何在C++中找到给定年份中一个月的第二个星期二?
- 为什么S [0]是C 中一个空字符串S的空字符串
- (c++)代码中一个非常奇怪的问题
- CUDA中一个简单Z缓冲区的实现
- 如何在模拟期望中调用const std::function<>&参数?
- 根据OPENCV中一个通道中的一个值,查找最大颜色像素
- Qt中一个类似事件系统的信号/槽,没有分叉-C++
- C++中一个特殊的Person类
- 首选钻石继承中一个类的变量
- 存储在硬盘中一个CVSVM对象
- C++中一个类的多个实现
- c++中一个简单光线跟踪器的问题
- 谷歌嘲笑——至少是众多期望中的一个