【问题标题】:Matching a struct in gmock匹配 gmock 中的结构
【发布时间】:2015-02-21 03:46:22
【问题描述】:

我正在尝试在与结构匹配的地方进行简单的 gmock 测试。但是,我在 gmock 内部遇到编译器错误。

作为序言,我想指出我已经定义了operator ==,我可以使用== 自己比较这些结构。定义为:

inline bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    return true;
}

这是g++给出的错误:

In file included from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-spec-builders.h:75:0,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock.h:61,
                 from /unit_test/tests.cpp:3:
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h: In instantiation of ‘bool testing::internal::EqMatcher<Rhs>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = MyStruct&; Rhs = MyStruct]’:
/unit_test/tests.cpp:115:1:   required from here
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: error: no match for ‘operator==’ in ‘lhs == ((const testing::internal::EqMatcher<MyStruct>::Impl<MyStruct&>*)this)->testing::internal::EqMatcher<MyStruct>::Impl<MyStruct&>::rhs_’
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: note: candidates are:
In file included from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-param-util.h:45:0,
                 from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/gtest-param-test.h:192,
                 from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/gtest.h:62,
                 from /unit_test/tests.cpp:2:
/unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-linked_ptr.h:213:6: note: template<class T> bool testing::internal::operator==(T*, const testing::internal::linked_ptr<T>&)
/unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-linked_ptr.h:213:6: note:   template argument deduction/substitution failed:
In file included from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-spec-builders.h:75:0,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock.h:61,
                 from /unit_test/tests.cpp:3:
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: note:   mismatched types ‘T*’ and ‘MyStruct’
In file included from /ifs/MyStruct.h:14:0,
                 from /unit_test/mocks.h:1,
                 from /unit_test/tests.cpp:1:

接下来是我定义的其他operator ==s 的巨大列表,它们与我创建的结构无关。比如:

 /ifs/types.h:2123:5: note: bool operator==(EnumType, const string&)
 /ifs/types.h:2123:5: note:   no known conversion for argument 1 from ‘MyStruct’ to ‘EnumType’
...etc

我的模拟方法定义为:

MOCK_METHOD1(send, int(MyStruct& data));

我正在尝试使用这个来匹配:

MyStruct data;
if(data == data); // this compiles fine
EXPECT_CALL(mockObj, send(data)); // this does not compile, why?

【问题讨论】:

    标签: c++ gmock


    【解决方案1】:

    似乎operator== 在 Google Mock 匹配器正在寻找的命名空间中不可用。您需要在全局命名空间中声明它或命名空间MyStruct 定义在中(因此依赖于参数的查找工作)。您不能将它放在与 MyStruct 定义的名称不同的非全局名称空间中,或者作为夹具的成员。

    【讨论】:

    • 谢谢,这行得通。我之前确实在全局命名空间中有operator ==,但看起来它必须在MyStructs 命名空间中。
    【解决方案2】:

    您可以使该方法在测试命名空间中可用,如下所示:

    namespace testing::internal {
    bool operator==(const MyStruct& lhs, const MyStruct& rhs)
    {
        return lhs.foo == rhs.foo;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      相关资源
      最近更新 更多