首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匹配gmock中的结构

匹配gmock中的结构
EN

Stack Overflow用户
提问于 2015-02-21 03:46:22
回答 2查看 4.3K关注 0票数 0

我试图得到一个简单的gmock测试,使之与结构匹配。但是,我在gmock的内部中得到了编译器错误。

首先,我要指出,我已经定义了operator ==,我可以使用==来比较这些结构。定义为:

代码语言:javascript
复制
inline bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    return true;
}

以下是g++给出的错误:

代码语言:javascript
复制
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 ==的巨大列表,它们与我创建的结构无关。比如:

代码语言:javascript
复制
 /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

我的模拟方法被定义为:

代码语言:javascript
复制
MOCK_METHOD1(send, int(MyStruct& data));

我试着用这个来匹配:

代码语言:javascript
复制
MyStruct data;
if(data == data); // this compiles fine
EXPECT_CALL(mockObj, send(data)); // this does not compile, why?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-21 09:08:53

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

票数 2
EN

Stack Overflow用户

发布于 2020-01-21 09:13:43

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

代码语言:javascript
复制
namespace testing::internal {
bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    return lhs.foo == rhs.foo;
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28641866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档