Вопрос

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!

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top