我正在尝试使用 google mock 在纯虚拟类中模拟一个函数。这是我的代码:
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
这里要删除的函数是什么?问题似乎是由带有 SetArgPointee 的 EXPECT_CALL 引起的,但我不知道它有什么问题。
最佳答案
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
类缺少复制构造函数和赋值运算符。添加这些将修复编译错误。话虽如此,我不清楚您编写的测试的目的。
关于c++ - 尝试模拟纯虚拟类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49275349/