Google模拟使用unique_ptr对象提供内存泄漏问题
我是Google测试的新手。 带有调用类的派生类
class Base
{
public:
virtual ~Base() {}
virtual int target_method() = 0;
}
class DerivedClass : public Base
{
public:
virtual ~DerivedClass () {}
int target_method() override;
}
class CallingClass
{
public:
CallingClass(std::unique_ptr<DerivedClass> _drivedClass);
private:
std::unique_ptr<DerivedClass> drivedClass;
}
我有一个基类,并从我的测试中
class MockDerivedClass : public DerivedClass
{
public:
MOCK_METHOD0(target_method, int());
}
TEST(TestGroup, Test1)
{
std::unique_ptr<MockDerivedClass> mockClass(new MockDerivedClass());
EXPECT_CALL(*mockClass, target_method()).WillRepeatedly(Return(1));
CallingClass callingClass(std::move(mockClass));
callingClass.callSomthing();
EXPECT_EQ(_ , _);
}
:测试运行良好,并且模拟方法的调用如预期。在测试结束时,我会遇到此错误:
错误:此模拟对象(在测试程序中使用,在TestGroup.test1中使用),但绝不是。它的地址是 @0x5585D93E4770。 错误:在程序退出时发现1个泄漏的模拟对象。当对象被破坏时,对模拟对象的期望得到验证。泄漏模拟意味着其期望未验证,这通常是测试错误。如果您真的打算泄漏模拟,则可以使用Testing :: Mock :: allow -leak(mock_object)抑制此错误,也可以使用假或存根代替模拟。
测试结束时,是否可以做任何抑制或解决此问题的事情?
I am new to Google Test.
I have one base class and derived class with calling class
class Base
{
public:
virtual ~Base() {}
virtual int target_method() = 0;
}
class DerivedClass : public Base
{
public:
virtual ~DerivedClass () {}
int target_method() override;
}
class CallingClass
{
public:
CallingClass(std::unique_ptr<DerivedClass> _drivedClass);
private:
std::unique_ptr<DerivedClass> drivedClass;
}
From my Test:
class MockDerivedClass : public DerivedClass
{
public:
MOCK_METHOD0(target_method, int());
}
TEST(TestGroup, Test1)
{
std::unique_ptr<MockDerivedClass> mockClass(new MockDerivedClass());
EXPECT_CALL(*mockClass, target_method()).WillRepeatedly(Return(1));
CallingClass callingClass(std::move(mockClass));
callingClass.callSomthing();
EXPECT_EQ(_ , _);
}
The test is running fine and the mock method is being called as expected. At the end of test I am getting this error:
ERROR: this mock object (used in test TestGroup.Test1) should be deleted but never is. Its address is @0x5585d93e4770.
ERROR: 1 leaked mock object found at program exit. Expectations on a mock object is verified when the object is destructed. Leaking a mock means that its expectations aren't verified, which is usually a test bug. If you really intend to leak a mock, you can suppress this error using testing::Mock::AllowLeak(mock_object), or you may use a fake or stub instead of a mock.
Is there anything can be done at end of test to suppress this or to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
uniqe_ptr
,模拟对象的所有者变为calleClass
。泄漏检测期望模拟频道
在您的测试中被摧毁,而不是在此之后。请注意,Gmock会在模拟对象破坏时验证对模拟对象的期望。请参阅此
您可以避免使用唯一的指针并在测试中构造模拟对象,然后将其地址传递到您的
CalleClass
,或在测试结束时添加此地址:这会验证并删除<<<<代码>模拟频道,并且仅在成功时才返回true。
By using a
uniqe_ptr
, the owner of the mock object becomes theCallingClass
. The leak detection expectsmockClass
to be destructed within your TEST, not possibly after that.Note that gMock will verify the expectations on a mock object when it is destructed. See this reference.
You can either avoid using a unique pointer and construct the mock object in your test and just pass its address to your
CallingClass
, or add this at the end of your test:This verifies and removes the expectations on
mockClass
and returns true if and only if successful.