Overloading function with google test

In my TestMock class I have two mock methods

  1. MOCK_CONST_METHOD0(function1, const bool&());
  2. MOCK_METHOD0(function1, bool&());

In the test fixture I want to call the second mock, but my test is calling the first.

    EXPECT_CALL(*testMock, function1());

What I must to write in EXPECT_CALL for calling second mock?

EXAMPLE:

class TestA
{
public:
    virtual ~ TestA() {}
    bool foo1()
    {
        return function1();
    }
    virtual bool& function1() = 0;
    virtual const bool& function1() const = 0;
};


class TestMock : public TestA
{
public:
    MOCK_CONST_METHOD0(function1, const bool&());
    MOCK_METHOD0(function1, bool&());
    virtual ~ TestMock () {}

};
class TestConfiguration : public :: testing :: Test
{
    void SetUp()
    {


    }

    void TearDown()
    {
        delete testMock;
    }

public:
TestMock *testMock;
};
TEST_F(TestConfiguration, testFooTEST)
{
    testMock = new TestMock();

    EXPECT_CALL(*testMock, function1());

    testMock->foo1();
}

Upvotes: 0

Views: 1526

Answers (1)

Tobias Wollgam
Tobias Wollgam

Reputation: 789

From CookBook at https://github.com/google/googlemock/blob/master/googlemock/docs/v1_5/CookBook.md#selecting-between-overloaded-functions

TestMock testMock;

EXPECT_CALL(testMock, function1())         // The non-const function1().
    .WillOnce(Return(true));
EXPECT_CALL(Const(testMock), function1())  // The const function1().
    .WillOnce(Return(false));

Upvotes: 1

Related Questions