gmock


GMock testing destructor calls


I've read the section in the gmock cookbook on Mocking Destructors but I'm having no luck getting it to work. My code is almost exactly what the doc says:
class MockFoo : public Foo {
public:
MockFoo() {}
MOCK_METHOD0(destroyMockFoo, void());
virtual ~MockFoo() { destroyMockFoo(); }
};
TEST_F(DestructorTest, shouldFail) {
MockFoo* foo = new MockFoo();
EXPECT_CALL(*foo, destroyMockFoo());
}
But when I run the code, the test passes with no errors. I do get the error at the end of the test output about the leaked object:
DestructorTest.cpp:149: ERROR: this mock object (used in test DestructorTest.shouldFail) should be deleted but never is. Its address is #0x8178790.
ERROR: 1 leaked mock object found at program exit.
but this is not what I want nor is it what the doc says should happen.
So what am doing wrong?
You're not doing anything wrong, necessarily. The problem here is that gmock expectations are verified in the destructor of the mock object, so if the destructor is never called, then the expectations are never verified. This is what's happening in your example.
The easy way to get around this is to explicitly verify the expectations on the mock:
Mock::VerifyAndClearExpectations(foo);
This will give you the behavior that you're looking for.
Delete foo object at the end of test so that it will not give object leak.

Related Links

gmock ReturnRef returns compilation error
How to call function pointer which is passed to Mock method?
In gmock, Is there a way to have mock object return an instance of an user defined object?
GMock testing destructor calls
set EXPECT_CALL to redirect the call to the original method
Capture GMOCK string parameter
Mocking non-virtual method generating errors
gmock matcher doesn't match my arguments by reference
What did you do to solve gmock you mentioned (link enclosed)?
how to set custom ref-variable in gmock

Categories

HOME
assembly
wakanda
apache-jena
redux-form
apache-spark-mllib
mstest
socket.io
alarm
google-cloud-functions
nsbundle
dojo
jquery-ui
gluon-mobile
google-schemas
gson
docx
esoteric-languages
google-content-api
asp.net-core-webapi
sar
inno-download-plugin
mongoid6
tosca
epsilon
short-url
displaytag
interpreter
intersystems-cache
exchangewebservices
quantlib
r-grid
android-sugarorm
contacts
vue-chartjs
engine.io
searchkit
password-protection
recurrent-neural-network
odoo-website
microsoft-metro
clang++
httrack
automapper-5
clarifai
geotiff
custom-font
static-cast
swingworker
sandcastle
docpad
custom-lists
webjars
iotivity
kango-framework
bizagi
data-protection
thread-sleep
concurrentmodification
avisynth
lazybones
phonertc
mod-proxy
mirror
botan
zend-currency
broadband
adehabitathr
easyrtc
ray
renderer
decoupling
qscrollarea
embedded-fonts
printdialog
alice
robotlegs
boost-msm
usertype
floating-point-exceptions
htdocs
event-receiver
cloudfiles
diagrams
aspmenu
eclipse-marketplace
zen
django-piston
lobo-cobra
mathematical-notation
cuda.net
escrow
sqlobject
appliance

Resources

Encrypt Message