I'm trying to add unit tests and mocks to a legacy project. As part of this I partially mocked one of the classes, mocking just the methods that I need in the class I'm currently testing. I then pass this mock in to the class for dependency injection and delete it in the destructor, however when I run the test it complains:
ERROR: this mock object (used in test xxxx) should be deleted but never is. Its address is @0000000004208AD0.
ERROR: 1 leaked mock object found at program exit.
When debugging through the code I can see that delete is being called on the mock object in the destructor, but it's still complaining. If I call delete again at the end of the test I get an SEH exception thrown because I'm trying to delete memory that's already been de-allocated, but if I comment out the delete in the destructor then it works (but this would obviously cause a memory leak)
GoogleTest: Mock object should be deleted but never is. UNSOLVED GoogleTest: Mock object should be deleted but never is. This topic has been deleted. Only users with topic management privileges can see it. it's the first time to work with GoogleTest.
When a mock object is destructed, gMock automatically verifies that all expectations on it have been satisfied. Here’s an example: using::testing::Return;// #1TEST(BarTest,DoesThis){MockFoofoo;// #2ON_CALL(foo,GetSize())// #3.
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.
Using Mocks in Tests The typical work flow is: Import the gMock names you need to use. All gMock symbols are in the testingnamespace unless they are macros or otherwise noted. Create the mock objects. Optionally, set the default actions of the mock objects. Set your expectations on the mock objects (How will they be called? What will they do?).
The issue is that I didn't declare the destructor virtual, so in the actual code it was calling the base class destructor and not my mock destructor.
This is probably obvious to some based on my last statement about it working when I deleted the mock in the test, but it took me a while to figure it out until I found this link, so I thought I'd share it on SO as well http://wahz.blogspot.ca/2014/08/google-mock-dammit-gets-me-every-time.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With