c++ - 模板模拟上的 EXPECT_CALL
问题描述
我的模拟定义如下:
template<typename T>
class ParseTreeMock : public ParseTreeInterface<T> {
public:
MOCK_METHOD1(fillConfigTree, void(std::string const&));
MOCK_METHOD1_T(getProperty, T(std::string const&));
ParseTreeMock(): parseTree(std::make_unique<pt::ptree>()) {
}
static std::unique_ptr<ParseTreeInterface<T>> getDefaultTree() {
return std::make_unique<ParseTreeMock<T>>();
}
private:
std::unique_ptr<pt::ptree> parseTree;
};
稍后在测试用例中创建:
class ConfigTest : public ::testing::Test {
protected:
std::unique_ptr<ParseTreeInterface<std::string>> treeMock;
virtual void SetUp() {
treeMock = ParseTreeMock<std::string>::getDefaultTree();
}
};
我想在 getProperty 方法上设置返回特定值:
EXPECT_CALL(*treeMock, getProperty("miniReaderConfig.cacheConfig.cacheOnOff")).willOnce(Return(false));
我得到错误:
In file included from ./lib/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:43:0,
from ./lib/googletest/googlemock/include/gmock/gmock.h:61,
from ./test/UT/Mocks/ParseTreeMock.hpp:2,
from test/UT/Configuration/ConfigTest.cpp:1:
test/UT/Configuration/ConfigTest.cpp: In member function ‘virtual void ConfigTest_CreateConfigurationWithoutErrors_Test::TestBody()’:
./lib/googletest/googlemock/include/gmock/gmock-spec-builders.h:1844:12: error: ‘class miniReader::Configuration::ParseTreeInterface<std::__cxx11::basic_string<char> >’ has no member named ‘gmock_getProperty’; did you mean ‘getProperty’?
((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
任何带有错误解释的解决方案都值得赞赏。
解决方案
treeMock
变量需要是类型 ,然后std::unique_ptr<ParseTreeMock<std::string>>
静态方法需要看起来像这样
static std::unique_ptr<ParseTreeMock<T>> getDefaultTree()
{
return std::make_unique<ParseTreeMock<T>>();
}
通常,您在测试中实例化一个实现接口的类,然后将该实例传递给您正在测试的类,并使用EXPECT_CALL
s 确保您正在测试的类调用模拟对象上的回调。
与您遇到的错误无关,但WillOnce
需要将第一个字母大写。此外,由于您将模板变量设置为std::string
,EXPECT_CALL
因此不能期望返回布尔值。
这为我编译:
namespace pt { struct ptree {};}
template<typename T>
class ParseTreeInterface
{
public:
virtual void fillConfigTree(std::string const&) = 0;
virtual T getProperty(std::string const&) = 0;
};
template<typename T>
class ParseTreeMock : public ParseTreeInterface<T> {
public:
MOCK_METHOD1(fillConfigTree, void(std::string const&));
MOCK_METHOD1_T(getProperty, T(std::string const&));
ParseTreeMock(): parseTree(std::make_unique<pt::ptree>()) {
}
static std::unique_ptr<ParseTreeMock<T>> getDefaultTree()
{
return std::make_unique<ParseTreeMock<T>>();
}
private:
std::unique_ptr<pt::ptree> parseTree;
};
class ConfigTest : public ::testing::Test {
protected:
std::unique_ptr<ParseTreeMock<std::string>> treeMock;
virtual void SetUp() {
treeMock = ParseTreeMock<std::string>::getDefaultTree();
}
};
TEST_F(ConfigTest, test)
{
EXPECT_CALL(*treeMock, getProperty("miniReaderConfig.cacheConfig.cacheOnOff")).WillOnce(::testing::Return(""));
}
推荐阅读
- java - 在运行时动态过滤火花列
- github - 我是 githib 新手,在合并分支时遇到问题,导致删除
- r - R:如何转置和合并表格
- authentication - WebAPI - 令牌端点和 API 端点的单独部署
- java - 有没有办法通过 @Autowired 注释在另一个类中创建同一类的多个实例?
- python - python selenium - chromedriver 在一个会话中更改代理
- oracle - 当我想显示具有最多部门的块时,为什么同时显示块号?
- excel - 如何使用匹配函数来获取跨列的多个值的位置
- c - C中的静态成员
- assembly - 如何比较 x86-64 中的两个寄存器