Google Mock Destructor
Asked Answered
A

1

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?

Astrophotography answered 18/5, 2013 at 17:18 Comment(8)
There's no (Default) action specified for Die() or any of the other mocked methods ...Horn
According to the documentation, I shouldn't need to specify a default action, since all of my methods use one of the primitive types. Am I missing something?Astrophotography
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.Astrophotography
Google: "Hey, destructor, your mother wears combat boots!" Destructor: "sob".Sava
maybe your mockSymbol object was not created properly? You can add EXPECT_TRUE(mockSymbol != NULL) to checkCheju
@user1743952 If you have solved this problem, you should post the answer as an answer, not a comment.Goy
@Kazark I couldn't do that when I originally posted the question due to new account restrictions.Astrophotography
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.Amatol
A
4

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

Astrophotography answered 3/7, 2014 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.