std :: function不是来自' std :: reference_wrapper< _Tp>'

时间:2018-05-24 09:59:56

标签: c++ unit-testing googlemock gmock

尝试单元测试类的私有回调方法。使用我可以模拟的函数注册回调,然后使用EXPECT_CALL和SaveArg我可以捕获到std :: function测试成员的回调函数并调用它进行测试。这很好。

当回调方法本身将std :: function作为参数时,问题就开始了。

生产代码:

我试图测试的私有回调方法:

void
MyClass::callbackMethod(const Callback callback)

作为callbackMethod参数的回调:

typedef std::function< void(void) > Callback;

绑定回调的公共函数:

void
MyClass::initialize(const Callback callback)
{
      m_SomeClass->registerCallback(std::bind(&MyClass::callbackMethod, this, callback));
}

测试代码:

让我试图从寄存器函数中保存回调参数的类memeber:

std::function< void(const Callback callback) > m_Callback;

这里应该发生什么:

EXPECT_CALL(*m_SomeClassMock, registerCallback(_))
         .WillOnce(SaveArg< 0 >(&m_Callback));

出现以下错误:

...
/usr/include/c++/6/functional:1999:2: note: candidate: template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Functor>) [with _Functor = _Functor; _Res = void; _ArgTypes = {std::function<void(void)>}]
  operator=(reference_wrapper<_Functor> __f) noexcept
  ^~~~~~~~
/usr/include/c++/6/functional:1999:2: note:   template argument deduction/substitution failed:
In file included from .../ext-cppunit-google-mock-dev/5/workspace/usr/include/gmock/gmock.h:65:0,
                 from .../SomeClassMock.hpp:15,
                 from .../MyTest.cpp:16:
.../gmock/gmock-more-actions.h:172:12: note:   'std::function<void(void)>' is not derived from 'std::reference_wrapper<_Tp>'
   *pointer = ::testing::get<k>(args);
   ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

不确定导致此错误的原因可能是在最后一行错误中可能应该:

std::function<void(std::function<void(void)>)>

给出了一些线索?

0 个答案:

没有答案