为什么 GoogleMock 报告与完全相同的参数值不匹配?
why GoogleMock reports not match with exact same arguments value?
纯虚class
class ConfigGetter {
public:
virtual const std::string Get(const char *name,
const char *default_value) const = 0;
};
模拟
class MockConfigGetter : public engine::ConfigGetter {
public:
MOCK_CONST_METHOD2(Get, const std::string(const char *, const char *));
};
测试用例
TEST(ConfigTest, NormalString) {
NiceMock<MockConfigGetter> getter;
EXPECT_CALL(getter, Get("SERVER_LISTEN_PORT", ""))
.WillRepeatedly(Return("80"));
// Pass
ASSERT_EQ("80", getter.Get("SERVER_LISTEN_PORT", ""));
// GetNewConfig internal call getter->Get("SERVER_LISTEN_PORT", "")
auto f = engine::GetNewConfig(&getter);
ASSERT_NE(f, nullptr);
// Failure
ASSERT_EQ(80, f->listen_port);
}
以及不错的模拟报告:
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_CALL 完全相同。不知道为什么第二次调用匹配不上
发生这种情况是因为 gmock 比较参数的值,即指针。要比较字符串,您需要添加相应的匹配器,即
EXPECT_CALL(getter, Get(StrEq("SERVER_LISTEN_PORT"), StrEq("")))
.WillRepeatedly(Return("80"));
纯虚class
class ConfigGetter {
public:
virtual const std::string Get(const char *name,
const char *default_value) const = 0;
};
模拟
class MockConfigGetter : public engine::ConfigGetter {
public:
MOCK_CONST_METHOD2(Get, const std::string(const char *, const char *));
};
测试用例
TEST(ConfigTest, NormalString) {
NiceMock<MockConfigGetter> getter;
EXPECT_CALL(getter, Get("SERVER_LISTEN_PORT", ""))
.WillRepeatedly(Return("80"));
// Pass
ASSERT_EQ("80", getter.Get("SERVER_LISTEN_PORT", ""));
// GetNewConfig internal call getter->Get("SERVER_LISTEN_PORT", "")
auto f = engine::GetNewConfig(&getter);
ASSERT_NE(f, nullptr);
// Failure
ASSERT_EQ(80, f->listen_port);
}
以及不错的模拟报告:
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_CALL 完全相同。不知道为什么第二次调用匹配不上
发生这种情况是因为 gmock 比较参数的值,即指针。要比较字符串,您需要添加相应的匹配器,即
EXPECT_CALL(getter, Get(StrEq("SERVER_LISTEN_PORT"), StrEq("")))
.WillRepeatedly(Return("80"));