Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock Destructor

I'm trying to become familiar with Google's mocking framework so I can more easily apply some TDD to my C++ development. I have the following interface:

#include <string>

class Symbol {
public:
    Symbol (std::string name, unsigned long address) {}
    virtual ~Symbol() {}
    virtual std::string getName() const = 0;
    virtual unsigned long getAddress() const = 0;
    virtual void setAddress(unsigned long address) = 0;
};

I want to verify that the destructor is called when an instance is deleted. So I have the following MockSymbol class:

#include "gmock/gmock.h"
#include "symbol.h"

class MockSymbol : public Symbol {
    public:
        MockSymbol(std::string name, unsigned long address = 0) :
            Symbol(name, address) {}
        MOCK_CONST_METHOD0(getName, std::string());
        MOCK_CONST_METHOD0(getAddress, unsigned long());
        MOCK_METHOD1(setAddress, void(unsigned long address));
        MOCK_METHOD0(Die, void());
        virtual ~MockSymbol() { Die(); }
};

Note: I've omitted the include guards in the above but they are in my header files.

I haven't been able to get to a point where I'm actually testing anything yet. I have the following:

#include "gmock/gmock.h"
#include "MockSymbol.h"

TEST(SymbolTableTests, DestructorDeletesAllSymbols) {
    ::testing::FLAGS_gmock_verbose = "info";
    MockSymbol *mockSymbol = new MockSymbol("mockSymbol");
    EXPECT_CALL(*mockSymbol, Die());
}

When I execute my test runner, my other tests execute and pass as I expect them to. However, when the above test executes I get the following error:

SymbolTableTests.cpp:11: EXPECT_CALL(*mockSymbol, Die()) invoked Segmentation fault (core dumped)

I've spent the last few hours searching Google and trying different things, but to know avail. Does anyone have any suggestions?

like image 581
Bobby Vandiver Avatar asked May 18 '13 17:05

Bobby Vandiver


1 Answers

I found that setting gtest_disable_pthreads to ON in my build config solves the problem.

like image 68
Bobby Vandiver Avatar answered Sep 27 '22 18:09

Bobby Vandiver