Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

gmock

How to invoke two different functions when a mocked function is called in the tested function in "Test suite"?

Details: A mocked function is called twice in a tested function. When it is called the first time, it should invoke one function (a local function in test suite) and when called the second time, it should invoke another function (another local function in test suite).

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

like image 954
udhai_coder Avatar asked Nov 30 '12 08:11

udhai_coder


1 Answers

You should use WillOnce.

Something like this (not tested) :

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 ) );
}
like image 123
BЈовић Avatar answered Sep 19 '22 13:09

BЈовић