C++ 为什么GoogleMock报告与完全相同的参数值不匹配?
纯虚拟类C++ 为什么GoogleMock报告与完全相同的参数值不匹配?,c++,googlemock,C++,Googlemock,纯虚拟类 类配置器{ 公众: 虚拟常量std::字符串Get(常量字符*名称, 常量字符*默认值)常量=0; }; 嘲弄 class MockConfigGetter:public engine::ConfigGetter{ 公众: MOCK_CONST_METHOD2(Get,CONST std::string(CONST char*,CONST char*)); }; 测试用例 TEST(ConfigTest,NormalString){ 奈西莫克吸气剂; 期望调用(getter、Get
类配置器{
公众:
虚拟常量std::字符串Get(常量字符*名称,
常量字符*默认值)常量=0;
};
嘲弄
class MockConfigGetter:public engine::ConfigGetter{
公众:
MOCK_CONST_METHOD2(Get,CONST std::string(CONST char*,CONST char*));
};
测试用例
TEST(ConfigTest,NormalString){
奈西莫克吸气剂;
期望调用(getter、Get(“服务器侦听端口”))
.遗嘱(返回(“80”);
//通过
ASSERT_EQ(“80”,getter.Get(“服务器侦听端口”);
//GetNewConfig内部调用getter->Get(“服务器\u侦听\u端口”,”)
auto f=engine::GetNewConfig(&getter);
断言(f,nullptr);
//失败
断言均衡器(80,f->侦听端口);
}
好的模拟报告:
Unexpected mock function call - returning default value.
Function call: Get(0x556fb6ea4860 pointing to "SERVER_LISTEN_PORT", 0x556fb6ea4843 pointing to "")
Returns: ""
Google Mock tried the following 1 expectation, but it didn't match:
/home/phillip/projects/engine-test/src/config/config-test.cpp:26: EXPECT_CALL(getter, Get("SERVER_LISTEN_PORT", ""))...
Expected arg #0: is equal to 0x556fb6ea2878 pointing to "SERVER_LISTEN_PORT"
Actual: 0x556fb6ea4860 pointing to "SERVER_LISTEN_PORT"
Expected arg #1: is equal to 0x556fb6ea27bc pointing to ""
Actual: 0x556fb6ea4843 pointing to ""
Expected: to be called any number of times
Actual: called twice - satisfied and active
arg0和arg1与EXPECT\u调用完全相同。我不知道为什么第二个调用无法匹配。发生这种情况是因为gmock比较参数值,即指针。要比较字符串,您需要添加相应的匹配器,即
EXPECT_CALL(getter, Get(StrEq("SERVER_LISTEN_PORT"), StrEq("")))
.WillRepeatedly(Return("80"));