Googlemock is incorrectly reporting problem at test exit. What am I doing wrong? I have scoured the googlemock documentation, but there is no good complete example, and nothing describing the problem I am having.
The error I get is:
googlemocktest.cpp(53): ERROR: this mock object should be deleted but never is.
Its address is @0018FDC4.
ERROR: 1 leaked mock object found at program exit.
The code for a simple test is:
#include <string>
#include <iostream>
#include <memory>
#include "gmock/gmock.h"
class IBar
{
public:
virtual ~IBar() {}
virtual void b() = 0;
virtual int c(std::string) = 0;
};
class Foo
{
private:
IBar *bar_;
public:
Foo(IBar *bar);
int a();
};
Foo::Foo(IBar *bar)
: bar_(bar)
{
}
int Foo::a()
{
// bar_->b();
return bar_->c("hello");
}
class BarMock : public IBar
{
public:
MOCK_METHOD0(b, void());
MOCK_METHOD1(c, int(std::string));
};
using ::testing::Return;
void TestAFunctionInFoo()
{
try
{
BarMock barMock;
std::unique_ptr<Foo> newFoo(new Foo(&barMock));
EXPECT_CALL(barMock, b());
EXPECT_CALL(barMock, c("hello")).WillOnce(Return(42));
newFoo->a();
}
catch (std::exception& e)
{
std::cout << "Mock exception caught: " << e.what() << std::endl;
}
catch (...)
{
}
}
int main(int argc, char* argv[])
{
::testing::GTEST_FLAG(throw_on_failure) = true;
::testing::InitGoogleMock(&argc, &argv[0]);
TestAFunctionInFoo();
return 0;
}
I have verified with a debugger that IBar::~IBar() is indeed getting called. But I still get this error message. If I uncomment the call to IBar::b() in Foo::a(), then there is no test failure so the catch statement doesn’t get called. IBar::~IBar() is called, but there is no error message indicating a mock object isn’t deleted.
Thank you very, very much for your help!
It happens because you have enabled
throw_on_failureflag – as it’s mentioned in Google Mock ForDummies:Since an exception is thrown from destructor, mock object is never fully deleted and framework complains about it when program ends.
The simplest solution to this problem is to use Google Test as unit testing framework and have
throw_on_failureflag disabled (default state) – GTest automatically fails test if mock expectations are not met within.If you cannot use Google Test because you’re using another framework (e.g. CppUnit) you can rewrite your test body this way:
VerifyAndClearExpectations(void*)checks if given mock expectations are met – if not andthrow_on_failureflag is enabled it throws an exception.This way your mock object will be destroyed properly, and you will still have an exception if something has gone wrong.
BTW: Avoid using
catch (...) { }– it’s generally bad idea, which in most cases proves poor code quality.