Search code examples
c++googlemock

Verify parameter has same value for multiple expected calls in gmock


Given some mock function which takes at least one parameter:

MOCK_METHOD1(fun, void(int p));

How can i EXPECT_CALL two identical calls in terms of the value of parameter p? I don't care what the value of p actually is, as long as it is the same for both calls to the function fun. I have no way to predict the value of p in the test case.


Solution

  • Option #1

    EXPECT_CALL( mock, fun( testing::Truly( []( int p ) {
                     static int initial = p;
                     return initial == p;
                 } ) ) )
        .Times( 2 );
    

    Option #2

    int p = 0;
    testing::Sequence seq;
    EXPECT_CALL( mock, fun( _ ) )
        .InSequence( seq )
        .WillOnce( testing::SaveArg< 0 >( &p ) );
    EXPECT_CALL( mock, fun( testing::Eq( testing::ByRef( p ) ) ) )
        .Times( 1 )
        .InSequence( seq );