Search code examples
c++qtqt5googletestgooglemock

GoogleMock and QString argument


I'm trying to use gmock with custom string type.

I have a method with QString argument which I want to mock:

MOCK_METHOD1(getValue, int(QString key));

I set an expectation:

EXPECT_CALL(mock, getValue("someKey"));

Got an error:

error: no matching function for call to 'MyMock::gmock_getValue(const char[8])'

include/gmock/gmock.h:9339:20: note: in definition of macro 'GMOCK_EXPECT_CALL_IMPL_'
     ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
 note: in expansion of macro 'EXPECT_CALL'

...

gmock/gmock.h:9730:7: note:   no known conversion for argument 3 from 'const char [6]' to 'const testing::Matcher<const QString&>&'
       gmock_##Method(GMOCK_MATCHER_(tn, F, 1) gmock_a1, \

But this works:

EXPECT_CALL(mock, getValue(QString("someKey")));

How can I use string arguments without wrapping each string literal with QString() ?


Solution

  • It is due to "someKey" is not a QString, it is a const char[8] as reported by the error and Google Test / Mock requires that the 2 classes are the same.

    In the same way a compiler does not know if the value 10 is supposed to be int32 int64, uint32 or uint64, the same applies.