将 Google Mock 与 boost::bind 一起使用
我有一个类,其构造函数采用 Boost 函数,我想用 Google Mock 测试它。以下代码显示了一个示例类以及我对其进行测试的尝试:
MyClass.h:
#include <boost/function.hpp>
class MyClass
{
public:
MyClass(boost::function<void()> callback);
void callCallback();
private:
boost::function<void()> m_callback;
};
MyClassTest.cpp:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/bind.hpp>
#include "MyClass.h"
class CallbackMock
{
public:
MOCK_METHOD0(callback, void());
};
TEST(MyClassShould, CallItsCallback)
{
CallbackMock callbackMock;
MyClass myClass(boost::bind(&CallbackMock::callback, callbackMock));
EXPECT_CALL(callbackMock, callback()).Times(1);
myClass.callCallback();
}
Attempting to compiling MyClassTest.cpp in Visual Studio 2008给出以下错误:
...gmock/gmock-generate-function-mockers.h(76) :错误C2248: '测试::内部::FunctionMockerBase::FunctionMockerBase' : 无法访问私有成员 在课堂上声明 '测试::内部::FunctionMockerBase' 1>与 1> [ 1>
F=void(空洞)1> ] 1>
.../gmock-spec-builders.h(1656) :参见 声明 '测试::内部::FunctionMockerBase::FunctionMockerBase' 1>与 1> [ 1>
F=void(空洞)1> ] 1>
该诊断发生在 编译器生成的函数 '测试::内部::FunctionMocker::FunctionMocker(const 测试::内部::FunctionMocker &)'1>与 1> [ 1>
函数=void(void)1> ]
该错误源于包含 boost::bind 的行。用 void callback(){} 替换模拟方法可以消除编译错误(但也消除了 Google Mock 的使用,达不到目的)。我想要做的事情可以在不修改测试类的情况下实现吗?
I have a class whose constructor takes a Boost function, and I'd like to test it with Google Mock. The following code shows a sample class and my attempt to test it:
MyClass.h:
#include <boost/function.hpp>
class MyClass
{
public:
MyClass(boost::function<void()> callback);
void callCallback();
private:
boost::function<void()> m_callback;
};
MyClassTest.cpp:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/bind.hpp>
#include "MyClass.h"
class CallbackMock
{
public:
MOCK_METHOD0(callback, void());
};
TEST(MyClassShould, CallItsCallback)
{
CallbackMock callbackMock;
MyClass myClass(boost::bind(&CallbackMock::callback, callbackMock));
EXPECT_CALL(callbackMock, callback()).Times(1);
myClass.callCallback();
}
Attempting to compile MyClassTest.cpp in Visual Studio 2008 gives the following error:
...gmock/gmock-generated-function-mockers.h(76)
: error C2248:
'testing::internal::FunctionMockerBase::FunctionMockerBase'
: cannot access private member
declared in class
'testing::internal::FunctionMockerBase'
1> with 1> [ 1>
F=void (void) 1> ] 1>
.../gmock-spec-builders.h(1656) : see
declaration of
'testing::internal::FunctionMockerBase::FunctionMockerBase'
1> with 1> [ 1>
F=void (void) 1> ] 1>
This diagnostic occurred in the
compiler generated function
'testing::internal::FunctionMocker::FunctionMocker(const
testing::internal::FunctionMocker
&)' 1> with 1> [ 1>
Function=void (void) 1> ]
The error stems from the line containing boost::bind. Replacing the mocked method with void callback(){} eliminates the compile error (but also eliminates the use of Google Mock, defeating the purpose). Is what I'm trying to do possible without modifying the tested class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是 Google Mock 模拟不可复制——这是设计使然。当您尝试按值将其传递给
boost::bind
时,编译器无法生成复制构造函数。您应该在将模拟的地址传递给bind
时获取它:The reason is that Google Mock mocks are not copyable - that is by design. When you try to pass it into
boost::bind
by value, the compiler fails to generate the copy constructor. You should take the mock's address when passing it intobind
:我认为这一行是错误的:
MyClass myClass(boost::bind(&CallbackMock::callback, callbackMock));
最后一个参数应该是
&callbackMock
I think this line is wrong:
MyClass myClass(boost::bind(&CallbackMock::callback, callbackMock));
The last parameter should be
&callbackMock