使用 TypedEq() 匹配类型 std::vector<std::vector>

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

本文关键字:std vector lt gt 类型 TypedEq 使用      更新时间:2023-10-16

我有2个模拟方法

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));
}

我想期望call write_data以范围作为参数。我想要匹配该类型以避免歧义。我不想比较该论点的价值。

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

它给了我以下错误:

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_; 
                    ^

根据文档,TypedEq接受一个值,而不是匹配器。如果要简单地在静态上验证类型,请使用A Matcher:

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