匹配gmock中的结构
Matching a struct in gmock
我正试图在与结构匹配的地方进行一个简单的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 ==
的巨大列表,它们与我创建的结构无关。比如:
/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方法定义为:
MOCK_METHOD1(send, int(MyStruct& data));
我正试图用这个来匹配:
MyStruct data;
if(data == data); // this compiles fine
EXPECT_CALL(mockObj, send(data)); // this does not compile, why?
似乎operator==
在Google Mock matcher正在寻找的命名空间中不可用。您需要在全局命名空间中声明它,或者在中定义了命名空间MyStruct
(因此依赖于参数的查找有效)。不能将其放在与MyStruct
定义的名称空间不同的非全局名称空间中,也不能将其作为fixture的成员。
您可以在测试命名空间中使用该方法,如下所示:
namespace testing::internal {
bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
return lhs.foo == rhs.foo;
}
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 带有结构数据的 Gmock
- 匹配gmock中的结构
- Gmock -匹配结构