expect object removal

2,253 views
Skip to first unread message

StepLg

unread,
Nov 25, 2009, 5:58:48 AM11/25/09
to Google C++ Mocking Framework
I need to check if an object will correctly deleted after some
operations.

find out some help in cookbook: http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Destructors

but, when i try to implement this feature i take nothink than
correctly passed tests. For example:

$ cat tmp.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>


class Foo {
public:
virtual ~Foo() {}
};

class FooMock : public Foo {
public:
MOCK_METHOD0(Die, void());
virtual ~FooMock() { Die(); }
};

TEST(DieTest, destr)
{
FooMock * m = new FooMock();
EXPECT_CALL(*m, Die());
}

$ g++ tmp.cpp -lgtest -lgmock -lgmock_main -o tmp && ./tmp
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from DieTest
[ RUN ] DieTest.destr
[ OK ] DieTest.destr (0 ms)
[----------] 1 test from DieTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.

tmp.cpp:19: ERROR: this mock object (used in test DieTest.destr)
should be deleted but never is. Its address is @0x131ec80.
ERROR: 1 leaked mock object found at program exit.


How to implement gmock object and call expectation to fail test, when
an object doesn't deleted correctly? Or mock framework expects, that
i'll figure out such type of errors in "leacked mock" messages?

Edison Gustavo Muenz

unread,
Nov 25, 2009, 8:04:26 AM11/25/09
to Google C++ Mocking Framework
In your test


TEST(DieTest, destr)
{
       FooMock * m = new FooMock();
       EXPECT_CALL(*m, Die());
}

the object 'm' is never deleted, so I don't understand it exactly yet
--
Edison Gustavo Muenz

StepLg

unread,
Nov 25, 2009, 8:21:32 AM11/25/09
to Google C++ Mocking Framework
Yes! it's the simpliest example, that shows DieTest never fails! so, m
is never deleted, and Die() is never called. But test passed
successfully. That's my question: I want to fail test in this case.
How?

On Nov 25, 4:04 pm, Edison Gustavo Muenz <edisongust...@gmail.com>
wrote:

Edison Gustavo Muenz

unread,
Nov 25, 2009, 10:35:08 AM11/25/09
to Google C++ Mocking Framework
From what I know that is not possible, because the expects are actually done when the 'm' object is deleted (which is done only when the application is exiting).

I don'
--
Edison Gustavo Muenz

Edison Gustavo Muenz

unread,
Nov 25, 2009, 10:36:57 AM11/25/09
to Google C++ Mocking Framework
Wow, gmail actually sent the e-mail and I couldn't unwind it, sorry.

But I was adding:

I don't use GTest here at work, so I don't know how tied to GTest is Google Mock (or vice versa), so I can't help much in this area.

Vlad Losev

unread,
Nov 25, 2009, 2:31:52 PM11/25/09
to StepLg, Google C++ Mocking Framework

Essentially, yes. Google Mock doesn't know at the end of the test whether you are done with your mock object or intend to keep it for further use. Some people have to persist their mocks across different tests. Because the mock is not is destroyed at the end of your test, Google Mock can not verify expectations on it. It can confidently perform leak checks only at the end of the program, and you see the error message it prints then. It also causes your test to return a non-zero exit code, which many tools use as an indicator of failure.

You have several options to make sure expectations are verified:
  • Allocate your mock objects on the stack instead on the heap. At the end of the test they will go out of scope, be destroyed, and automatically verify expectations.
  • If you have to allocate on the heap, take care of deleting at the end of the test all heap-allocated mocks. A good heap checker can help here.
  • At the end of your test, force verification on mocks you suspect of being leaked. http://code.google.com/p/googlemock/wiki/CookBook#Forcing_a_Verification provides more information on this option.

Hope this helps,
Vlad

Zhanyong Wan (λx.x x)

unread,
Nov 26, 2009, 3:11:26 PM11/26/09
to StepLg, Google C++ Mocking Framework
On Wed, Nov 25, 2009 at 5:21 AM, StepLg <ste...@gmail.com> wrote:
> Yes! it's the simpliest example, that shows DieTest never fails! so, m
> is never deleted, and Die() is never called. But test passed
> successfully.

The TEST method passed, but your test program as a whole didn't pass.
The program exits with a non-zero code, which is the standard way of
saying "I failed".
--
Zhanyong

Zhanyong Wan (λx.x x)

unread,
Nov 26, 2009, 3:13:10 PM11/26/09
to Edison Gustavo Muenz, Google C++ Mocking Framework
Off-topic but I'm curious: what C++ testing framework do you use at
work and what do you like about it? I'm trying to learn if there's
something gtest can learn from. Thanks,

--
Zhanyong

Stephan Kountso

unread,
Nov 26, 2009, 4:13:12 PM11/26/09
to Google C++ Mocking Framework
Thank's a lot for answers. trying to rewrite my memory tests...

2009/11/26 Zhanyong Wan (λx.x x) <w...@google.com>:
--
С уважением,
Кунцьо Степан aka StepLg
Reply all
Reply to author
Forward
0 new messages