这可以通过两种方式实现:
- 带有自定义方法的执行器——创建一个类,具有相同的方法。然后使用
Invoke() 将调用传递给该对象
- 使用偏序调用 - 以不同的顺序创建不同的期望,如here 所述
带自定义方法执行器
类似这样的:
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();
}