我对纯虚方法和Google Mock做错了什么?

时间:2013-11-18 17:27:47

标签: c++ unit-testing googlemock

简介

我在使用Google Mock(v1.7.0)实现纯虚拟界面时遇到问题。

为了确定根本原因,我将纯虚拟接口SimpleMockSimple放在一起作为测试。我相信我在这里所做的与this Google Mock example一致。

然而,当我编译它时,我收到一个错误。为什么是这样?我做错了什么?

简单测试代码

#include "gmock/gmock.h"
#include "gtest/gtest.h"

class Simple
{
public:
    virtual ~Simple() {}
    virtual int Initialize()   = 0;
};

class MockSimple : public Simple
{
public:
    MOCK_METHOD0(Initialize, int());
};

TEST(Hello, MockSimple)
{
    MockSimple m;
    EXPECT_CALL(m, Initialize);
    m.Initialize();
}

编译命令

g++ -I../gmock/include/ -I../gmock/gtest/include  -c test.cpp -o test.o

错误输出

test.cpp: In member function ‘virtual void Hello_MockSimple_Test::TestBody()’:
test.cpp:20:5: error: ‘m.MockSimple::gmock_Initialize’ does not have class type

1 个答案:

答案 0 :(得分:6)

需要在EXPECT_CALL中调用的方法的参数列表(在本例中为空):)

EXPECT_CALL(m, Initialize());