I'm using Google Mock to specify a compatibility layer to an external API. In the external API, there are multiple ways to do some actions, so I want to specify that at least one (or preferably exactly one) expectation from a set of expectations are fulfilled. In pseudocode, this is what I want to do:
Expectation e1 = EXPECT_CALL(API, doFoo(...));
Expectation e2 = EXPECT_CALL(API, doFooAndBar(...));
EXPECT_ONE_OF(e1, e2);
wrapper.foo(...);
Is this possible to do using Google Mock?
This is possible to do in two ways :
Invoke()
to pass the call to that objectWith custom method executor
Something like this :
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 );
}
using partial order call
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With