10

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?

Bobby Vandiver
  • 306
  • 3
  • 11
  • There's no (Default) action specified for `Die()` or any of the other mocked methods ... – πάντα ῥεῖ May 18 '13 at 17:26
  • According to the [documentation](https://code.google.com/p/googlemock/wiki/CheatSheet#Setting_Default_Actions), I shouldn't need to specify a default action, since all of my methods use one of the primitive types. Am I missing something? – Bobby Vandiver May 18 '13 at 19:17
  • 4
    I'm running Cygwin on Windows 7. After some digging, I found that setting gtest_disable_pthreads to ON in my build config solves the problem. – Bobby Vandiver May 18 '13 at 20:16
  • 1
    Google: "Hey, destructor, your mother wears combat boots!" Destructor: "sob". – Pete Becker May 18 '13 at 21:29
  • maybe your mockSymbol object was not created properly? You can add EXPECT_TRUE(mockSymbol != NULL) to check – swang May 11 '14 at 16:22
  • @user1743952 If you have solved this problem, you should post the answer as an answer, not a comment. – Keith Pinson Jul 03 '14 at 17:33
  • @Kazark I couldn't do that when I originally posted the question due to new account restrictions. – Bobby Vandiver Jul 03 '14 at 21:00
  • It is probably not the cause of the crash, but you should add a call to delete MockSymbol. Otherwise, your destructor will never be called. – dannyhan12 Dec 03 '14 at 02:19

1 Answers1

4

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

Bobby Vandiver
  • 306
  • 3
  • 11