Search code examples
c++googletestgooglemock

Use TypedEq() to match the type std::vector<std::vector>


I have 2 mock method

struct temp_struct
{
int x;
};

using range = std::vector<std::vector<temp_struct>>;

Class MockA: public A
{
public:
   MOCK_METHOD1(write_data, int(int a, int b));
   MOCK_METHOD1(write_data, int(int a, const range &ranges));
}

I want to do expect call write_data with range as argument. I want to match the type to avoid ambiguity. I don't want to compare value of that argument.

TEST_F(MyTest, test1)
{
    ...
    EXPECT_CALL(MOCKA_obj, write_data(_, TypedEq<const range_t&>(_)))
       .Times(1)
       .WillOnce(Return(0));
    ...
}

It is giving me following error:

test.cc.cc:256:1:   required from here build/../gmock/include/gmock/gmock.h:5564:41: error: no match for 'operator==' (operand types are 'std::vector<std::vector<temp_struct> >' and 'const testing::internal::AnythingMatcher')
 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
                                         ^

build/../gmock/include/gmock/gmock.h:5544:20: note: in definition of macro 'GMOCK_IMPLEMENT_COMPARISON_MATCHER_'
         return lhs op rhs_; \
                    ^

build/../gmock/include/gmock/gmock.h:5564:41: note: candidates are:
 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
                                         ^

build/../gmock/include/gmock/gmock.h:5544:20: note: in definition of macro 'GMOCK_IMPLEMENT_COMPARISON_MATCHER_'
         return lhs op rhs_; \
                    ^

In file included from test.cc.cc:3:0:
build/../gmock/include/gtest/gtest.h:9173:6: note: template<class T> bool testing::internal::operator==(T*, const testing::internal::linked_ptr<T>&)
 bool operator==(T* ptr, const linked_ptr<T>& x) {
      ^


build/../gmock/include/gtest/gtest.h:9173:6: note:   template argument deduction/substitution failed:
In file included from build/../test.h:11:0,
                 from test.cc.cc:7:

build/../gmock/include/gmock/gmock.h:5564:41: note:   mismatched types 'T*' and 'std::vector<std::vector<temp_struct> >'
 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
                                         ^

build/../gmock/include/gmock/gmock.h:5544:20: note: in definition of macro 'GMOCK_IMPLEMENT_COMPARISON_MATCHER_'
         return lhs op rhs_; \
                    ^

Solution

  • According to the docs, TypedEq accepts a value, not a matcher. If you want to simply verify the type statically, use the A matcher:

    EXPECT_CALL(MOCKA_obj, write_data(_, A<const range_t&>()))