使用 google 测试重载函数

Overloading function with google test

在我的 TestMock 中 class 我有两个 mock 方法

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

在测试夹具中我想调用第二个模拟,但我的测试调用了第一个。

    EXPECT_CALL(*testMock, function1());

为了调用第二个 mock,我必须在 EXPECT_CALL 中写些什么?

示例:

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();
}

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

的 CookBook
TestMock testMock;

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