尝试模拟纯虚拟 class

Try to mock pure virtual class

我正在尝试使用 google 模拟在纯虚拟 class 中模拟一个函数。这是我的代码:

class I_Circle

{

private :

public: 

    virtual ~I_Circle() {}

    virtual void GetClone(I_Circle * k) = 0;
};


class Mock_I_Circle : public I_Circle
{
public:
    Mock_I_Circle() {}
    virtual ~Mock_I_Circle() {};

    MOCK_METHOD1(GetClone, void(I_Circle* k));

};

TEST(CircleTest, secondTest) {
    NiceMock<Mock_I_Circle> iCircle;
    Mock_I_Circle* pICircle = &iCircle;

    EXPECT_CALL(iCircle, GetClone(_))
        .WillOnce(
            SetArgPointee<0>(*pICircle)
        );

    iCircle.GetClone(pICircle);

    Mock::VerifyAndClear(&iCircle);
}

通过此实现,我收到以下错误:

Error   C2280   'testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<0,Mock_I_Circle,false>>::PolymorphicAction(const testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<0,Mock_I_Circle,false>> &)': attempting to reference a deleted function MockInterface   c:\source\googletest\googlemock\include\gmock\gmock-actions.h   1110        

这里要删除的是什么函数?问题似乎是 EXPECT_CALL 与 SetArgPointee 的结果,但我不知道它有什么问题。

来自Google Mock Cookbook

SetArgPointee() conveniently makes an internal copy of the value you pass to it, removing the need to keep the value in scope and alive. The implication however is that the value must have a copy constructor and assignment operator.

在您的例子中,Mock_I_Circle class 缺少复制构造函数和赋值运算符。添加这些将修复编译错误。话虽如此,我不清楚您编写的测试的目的。