0

我用 hippomocks 编写了一个单元测试,但在编译时出错。

编译器是VS 2010。

我该如何解决?

#include "hippomocks.h"
#include <functional>

using namespace HippoMocks;

struct A
{
    virtual void f(std::function<void (int)> arg);
};

int main(void)
{
    MockRepository mock;
    A* aptr = mock.Mock<A>();

    mock.ExpectCall(aptr, A::f);  // error

    return 0;
}

输出是:

main.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
c:\users\cong\project\test\test\hippomocks.h(466) : error C2593: 'operator <<' i
s ambiguous
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\ostream(2
06): could be 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Trai
ts>::operator <<(std::_Bool)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\users\cong\project\test\test\hippomocks.h(441): or       'std::ostrea
m &HippoMocks::operator <<(std::ostream &,const HippoMocks::NotPrintable &)'
        while trying to match the argument list '(std::ostream, std::tr1::functi
on<_Fty>)'
        with
        [
            _Fty=void (int)
        ]
        c:\users\cong\project\test\test\hippomocks.h(463) : while compiling clas
s template member function 'void HippoMocks::printArg<T>::print(std::ostream &,T
,bool)'
        with
        [
            T=std::tr1::function<void (int)>
        ]
        c:\users\cong\project\test\test\hippomocks.h(614) : see reference to cla
ss template instantiation 'HippoMocks::printArg<T>' being compiled
        with
        [
            T=std::tr1::function<void (int)>
        ]
4

1 回答 1

0

在这里增强@dascandy 的评论是他的方法的样子。包括后放置hippomocks.h

template<>
struct printArg<std::function<void (int)> >
{
    static inline void print(std::ostream &os, std::function<void (int)> arg, bool withComma)
    {
        if (withComma)
        {
            os << ",";
        }
        if (arg)
        {
            os << "true";
        }
        else
        {
            os << "false";
        }
    }
};

请注意,我没有测试这个示例,而是采用了我们的解决方案并将类型调整为原始帖子的示例。我很高兴知道这是否适合您。

于 2016-09-22T13:51:34.860 回答