我遵循以下方法:
QMap<QString, int> DefaultConfig::getConfig()
{
QMap<QString, int> result;
result.insert("Error", LOG_LOCAL0);
result.insert("Application", LOG_LOCAL1);
result.insert("System", LOG_LOCAL2);
result.insert("Debug", LOG_LOCAL3);
result.insert("Trace", LOG_LOCAL4);
return result;
}
我尝试编写可以返回在测试中准备好的QMap的模拟程序:
QMap<QString, int> DefaultConfig::getConfig() {
mock().actualCall("getConfig");
return ?
}
但我不知道如何模拟返回值?我想以以下方式在TEST
函数中使用模拟:
QMap<QString, int> fake_map;
fake_map.insert("ABC", 1);
mock().expectOneCall("getConfig").andReturnValue(fake_map);
我在CppUTest模拟文档中找不到这样的示例。我也知道,.andReturnValue
这种形式也行不通。
代替传递对象按值/-引用,而是传递by-pointer。
(我在std::map
这里使用的是-QMap
完全一样)
您可以通过return#####Value()
方法获得模拟的返回值。由于returnPointerValue()
返回a,void*
您必须将其强制转换为正确的指针类型。然后,您可以通过取消引用该指针来返回按值。
std::map<std::string, int> getConfig()
{
auto returnValue = mock().actualCall("getConfig")
.returnPointerValue();
return *static_cast<std::map<std::string, int>*>(returnValue);
}
预期的返回值通过指针传递:
TEST(MapMockTest, mockReturningAMap)
{
std::map<std::string, int> expected = { {"abc", 123} };
mock().expectOneCall("getConfig").andReturnValue(&expected);
auto cfg = getConfig();
CHECK_EQUAL(123, cfg["abc"]);
}
请不要,Pointer和ConstPointer之间是有区别的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句