【问题标题】:HippoMocks - mocking methods with std::vector fail to compileHippoMocks - 使用 std::vector 模拟方法无法编译
【发布时间】:2012-08-02 05:58:20
【问题描述】:

在尝试使用 HippoMocks(Cygwin、GCC 4.5.3、CppUnit)模拟接口时,其中一种方法会导致编译失败。进一步的分类表明,只有以 std::vector 作为参数的模拟方法才会失败。

例如

m_Mocks.ExpectCall(m_EmpSvcMock.get(), IEmployeeServiceProxy::GetEmployees); // compile error!
m_Mocks.ExpectCall(m_EmpSvcMock.get(), IEmployeeServiceProxy::AddEmployee); // compile OK!

在哪里

class IEmployeeServiceProxy
{
public:
    virtual ~IEmployeeServiceProxy() { }
    virtual void AddEmployee(const Employee&) = 0;
    virtual void GetEmployees(std::vector<Employee>&) = 0;
};

struct Employee
{
    boost::uuids::uuid Id;
    std::string Name;
};

编译器错误:

/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stream_iterator.h: In member function ‘std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = EmployeeServiceLib::Employee, _CharT = char, _Traits = std::char_traits<char>, std::ostream_iterator<_Tp, _CharT, _Traits> = std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >]’:
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/iterator:67:0,
                 from /usr/include/boost/uuid/string_generator.hpp:14,
                 from /usr/include/boost/uuid/uuid_generators.hpp:15,
                 from tests/../../RcfTestShared/IEmployeeService.hpp:7,
                 from tests/../IEmployeeServiceProxy.h:11,
                 from tests/ClientTest.h:13,
                 from tests/ClientTest.cpp:8:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_algobase.h:349:8:   instantiated from ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const EmployeeServiceLib::Employee*, _OI = std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >]’
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_algobase.h:404:70:   instantiated from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false, _II = const EmployeeServiceLib::Employee*, _OI = std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >]’
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_algobase.h:442:39:   instantiated from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false, _II = __gnu_cxx::__normal_iterator<const EmployeeServiceLib::Employee*, std::vector<EmployeeServiceLib::Employee> >, _OI = std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >]’
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_algobase.h:474:18:   instantiated from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const EmployeeServiceLib::Employee*, std::vector<EmployeeServiceLib::Employee> >, _OI = std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >]’
../../RCF/RCF-1.3.1/include/RCF/Tools.hpp:116:9:   instantiated from ‘std::ostream& std::operator<<(std::ostream&, const std::vector<_RealType>&) [with T = EmployeeServiceLib::Employee, std::ostream = std::basic_ostream<char>]’
../../hippomocks/HippoMocks/hippomocks.h:94:5:   instantiated from ‘static void printArg<T>::print(std::ostream&, T, bool) [with T = std::vector<EmployeeServiceLib::Employee>&, std::ostream = std::basic_ostream<char>]’
../../hippomocks/HippoMocks/hippomocks.h:170:5:   instantiated from ‘void ref_tuple<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>::printTo(std::ostream&) const [with A = std::vector<EmployeeServiceLib::Employee>&, B = NullType, C = NullType, D = NullType, E = NullType, F = NullType, G = NullType, H = NullType, I = NullType, J = NullType, K = NullType, L = NullType, M = NullType, N = NullType, O = NullType, P = NullType, std::ostream = std::basic_ostream<char>]’
tests/ClientTest.cpp:47:1:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stream_iterator.h:196:2: error: cannot bind ‘std::ostream_iterator<EmployeeServiceLib::Employee, char, std::char_traits<char> >::ostream_type’ lvalue to ‘std::basic_ostream<char>&&’
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ostream:579:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = EmployeeServiceLib::Employee]’

【问题讨论】:

  • 好吧,您现在已经删除了错误消息,但在我看到它们的短暂时间内,我认为问题在于它试图将 Employee 对象与 operator== 进行比较。您是否为 Employee 类定义了 operator==?只要为 T 定义了 operator==,std::vector 的 operator== 就可以工作。
  • @jahhaj:我已经将 HippoMocks 从他们的 svn 更新到了最新版本,但我现在遇到了不同的错误......
  • 不同的错误是什么?您也没有回答我的问题,您是否为员工定义了 operator==?我认为这对于 Employee 类的任何单元测试都非常重要。
  • 你应该定义operator==,POD与它无关。即使是 POD 类型,也不会自动定义 operator==。
  • @jahhaj:添加运算符没有帮助。还是一样的错误。

标签: c++ unit-testing mocking hippomocks


【解决方案1】:

可能是抱怨您没有为 Employee 定义 operator

要求您定义 operator== 和 operator

【讨论】:

  • 如果该类没有定义运算符
  • @dascandy:我有几个问题要问——我如何直接联系你而不是问关于 SO 的问题?
  • 您可以随时使用我的电子邮件地址 dascandy@gmail.com。我刚刚注意到 wiki 已经损坏 - 哎呀...我会先解决这个问题,它应该有大部分答案,但现在它只有一个数据库错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
相关资源
最近更新 更多