我正在尝试熟悉 Google 的模拟框架,以便我可以更轻松地将一些 TDD 应用到我的 C++ 开发中。我有以下界面:
#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;
};
我想验证在删除实例时是否调用了析构函数。所以我有以下 MockSymbol 类:
#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(); }
};
注意:我在上面省略了包含保护,但它们在我的头文件中。
我还没有达到我实际测试任何东西的地步。我有以下内容:
#include "gmock/gmock.h"
#include "MockSymbol.h"
TEST(SymbolTableTests, DestructorDeletesAllSymbols) {
::testing::FLAGS_gmock_verbose = "info";
MockSymbol *mockSymbol = new MockSymbol("mockSymbol");
EXPECT_CALL(*mockSymbol, Die());
}
当我执行我的测试运行程序时,我的其他测试会按照我的预期执行并通过。但是,当执行上述测试时,我收到以下错误:
SymbolTableTests.cpp:11: EXPECT_CALL(*mockSymbol, Die()) 调用分段错误(核心转储)
在过去的几个小时里,我一直在搜索谷歌并尝试不同的东西,但要知道是否有用。有没有人有什么建议?