How to set GMock EXPECT_CALL to call two different functions for a mocked function

How to call two different functions when the function "piece" is called in the function being tested?

More: In the tested function, the bullying function is called. When it is called for the first time, it must call one function (a local function in the test suite), and when called a second time, it must call another function (another local function in the test suite).

So how to set EXPECT_Call with "Invoke" for the above requirement?

+4
source share
1 answer

You must use WillOnce .

Something like this (not verified):

 struct A { MOCK_METHOD0( foo, void()); }; class A_Test : public ::testing::Test { A a; void bar1(){} void bar2(){} }; TEST_F( A_Test, test_1 ) { EXPECT_CALL( a, foo() ) .WillOnce( Invoke( this, &A_Test::bar1 ) ) .WillOnce( Invoke( this, &A_Test::bar2 ) ); } 
+6
source

All Articles