Googlemock di segnalazione erroneamente Errore all'uscita del test. Che cosa sto facendo di sbagliato?

StackOverflow https://stackoverflow.com/questions/8426507

  •  29-10-2019
  •  | 
  •  

Domanda

Googlemock sta segnalando erroneamente un problema all'uscita del test. Che cosa sto facendo di sbagliato? Ho setacciato la documentazione di Googlemock, ma non c'è un buon esempio completo e nulla che descriva il problema che sto riscontrando.

L'errore che ricevo è:

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.

Il codice per un semplice test è:

#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;
}

Ho verificato con un debugger che Ibar :: ~ ibar () viene davvero chiamato. Ma ricevo ancora questo messaggio di errore. Se sono in rovina la chiamata a ibar :: b () in foo :: a (), allora non vi è alcun fallimento del test, quindi la dichiarazione di cattura non viene chiamata. Ibar :: ~ ibar () viene chiamato, ma non c'è un messaggio di errore che indica che un oggetto finto non viene eliminato.

Grazie mille per il tuo aiuto!

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top