Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top