1

I am using Gmock for unit tests. I have function who receive protobuf message as an argument. The problem is that when I'm testing the function with expected value it gives me an error of missing operator==. I found similar problem here google-protocol-buffers-compare

class ClientReaderWriterMock : public ClientReaderWriterIf {
 public:
  virtual ~ClientReaderWriterMock() = default;
  MOCK_METHOD1(Write, bool(const Msg&));
  MOCK_METHOD1(Read, bool(Msg*));
};

TEST_F(controller_Test, receive_message) {
Msg msg;
.
.
.
EXPECT_CALL(*clientReaderWriterMockObj, Write(msg));
.
.
.
}

I receive the following error:

###>/third_party/googletest/googletest/include/gtest/gtest-matchers.h: In instantiation of ‘bool testing::internal::AnyEq::operator()(const A&, const B&) const [with A = Msg; B = Msg]’: /###>/third_party/googletest/googletest/include/gtest/gtest-matchers.h:549:18: required from ‘bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs, >::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = const Msg&; = Msg; D = testing::internal::EqMatcher; Rhs = Msg; Op = testing::internal::AnyEq]’ /###>/third_party/googletest/googletest/include/gtest/gtest-matchers.h:547:10: required from here /###>/third_party/googletest/googletest/include/gtest/gtest-matchers.h:211:60: error: no match for ‘operator==’ (operand types are ‘const Msg’ and ‘const Msg’) 211 | bool operator()(const A& a, const B& b) const { return a == b; } In file included from /###>/tests/unit/test.cpp:2: /###>/third_party/googletest/googletest/include/gtest/gtest.h:1535:13: note: candidate: ‘bool testing::internal::operator==(testing::internal::faketype, testing::internal::faketype)’ 1535 | inline bool operator==(faketype, faketype) { return true; } /###>/third_party/googletest/googletest/include/gtest/gtest.h:1535:24: note: no known conversion for argument 1 from ‘const Msg’ to ‘testing::internal::faketype’ 1535 | inline bool operator==(faketype, faketype) { return true; }

1 Answer 1

3

When an expect call is set on a function with some arguments, the Eq matcher is used (implicitly), so the line:

EXPECT_CALL(*clientReaderWriterMockObj, Write(msg));

is actually:

EXPECT_CALL(*clientReaderWriterMockObj, Write(Eg(msg)));

Eq matcher will try to invoke operator== (which is missing as you noticed). In such case, you can define your own matcher:

MATCHER_P(CustomMatcher, expected, "Msg doesn't match!") {
    // your comparision code here
    return arg.Field() == expected.Field();
}
[...]
EXPECT_CALL(*clientReaderWriterMockObj, Write(CustomMatcher(msg)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.