我尝试模拟一个简单的 C++ 有什么问题吗? googlemock 的方法?
根据 C++ 单元测试模式方法进行标准库调用,我正在测试驱动网络抽象类的开发。为了对进行标准 C 库调用(我无法模拟)来处理 BSD 套接字的代码进行单元测试,我定义了一个接口 ISocket
,我的实际实现 CSocket< /code> 和模拟
MockSocket
继承。
现在,我为 Network
类编写第一个单元测试,该类使用 ISocket
来完成繁重的工作:
#include "gmock/gmock.h"
#include "gtest/gtest.h"
// C standard library includes omitted
#include "MockSocket.h"
#include "Network.h"
using ::testing::Return;
namespace JrStream {
class NetworkTest : public ::testing::Test {
protected:
Network net;
ISocket * socket_ptr;
virtual void SetUp() {
socket_ptr = new MockSocket();
}
};
TEST_F(NetworkTest, InitCallsSocket) {
EXPECT_CALL((MockSocket)*socket_ptr, Socket(AF_INET, SOCK_STREAM, 0))
.Times(1)
.WillOnce(Return(5)); //fake file descriptor
ASSERT_TRUE(net.init(socket_ptr));
}
} // namespace
// gtest boilerplate main() omitted
但是我在编译时遇到此错误:
g++ -g -Wall -Lgtest-1.5.0/lib -Igmock-1.5.0/include -Igtest-1.5.0/include -I.. -I../../Debug/src -c -o NetworkTest.o NetworkTest.cc
NetworkTest.cc: In member function ‘virtual void JrStream::NetworkTest_InitCallsSocket_Test::TestBody()’:
NetworkTest.cc:35: error: no matching function for call to ‘JrStream::MockSocket::MockSocket(JrStream::ISocket&)’
MockSocket.h:16: note: candidates are: JrStream::MockSocket::MockSocket()
MockSocket.h:16: note: JrStream::MockSocket::MockSocket(const JrStream::MockSocket&)
这是我的接口和模拟看起来像:
namespace JrStream {
class ISocket {
virtual int Socket(int domain, int type, int protocol) = 0;
};
}
#include "gmock/gmock.h"
#include "ISocket.h"
namespace JrStream {
class MockSocket: public JrStream::ISocket {
MOCK_METHOD3(Socket, int(int domain, int type, int protocol));
};
}
知道我在这里做错了什么吗?
As per Patterns for unit testing a C++ method that makes a standard library call, I'm test-driving development of a network-abstracting class. In order to unit test code that makes standard C library calls (which I can't mock) to handle BSD sockets, I've defined an interface ISocket
from which both my real implementation CSocket
and mock MockSocket
inherit.
Now I write my first unit test for the Network
class, which uses an ISocket
for the heavy lifting:
#include "gmock/gmock.h"
#include "gtest/gtest.h"
// C standard library includes omitted
#include "MockSocket.h"
#include "Network.h"
using ::testing::Return;
namespace JrStream {
class NetworkTest : public ::testing::Test {
protected:
Network net;
ISocket * socket_ptr;
virtual void SetUp() {
socket_ptr = new MockSocket();
}
};
TEST_F(NetworkTest, InitCallsSocket) {
EXPECT_CALL((MockSocket)*socket_ptr, Socket(AF_INET, SOCK_STREAM, 0))
.Times(1)
.WillOnce(Return(5)); //fake file descriptor
ASSERT_TRUE(net.init(socket_ptr));
}
} // namespace
// gtest boilerplate main() omitted
But I get this error on compilation:
g++ -g -Wall -Lgtest-1.5.0/lib -Igmock-1.5.0/include -Igtest-1.5.0/include -I.. -I../../Debug/src -c -o NetworkTest.o NetworkTest.cc
NetworkTest.cc: In member function ‘virtual void JrStream::NetworkTest_InitCallsSocket_Test::TestBody()’:
NetworkTest.cc:35: error: no matching function for call to ‘JrStream::MockSocket::MockSocket(JrStream::ISocket&)’
MockSocket.h:16: note: candidates are: JrStream::MockSocket::MockSocket()
MockSocket.h:16: note: JrStream::MockSocket::MockSocket(const JrStream::MockSocket&)
And here is what my interface and mock look like:
namespace JrStream {
class ISocket {
virtual int Socket(int domain, int type, int protocol) = 0;
};
}
#include "gmock/gmock.h"
#include "ISocket.h"
namespace JrStream {
class MockSocket: public JrStream::ISocket {
MOCK_METHOD3(Socket, int(int domain, int type, int protocol));
};
}
Any ideas what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在 TestBody 中的某处调用 MockSocket 的复制构造函数,而无需进行强制转换。
Looks like you're invoking the copy constructor for MockSocket somewhere in TestBody without a cast.
我终于把它整理好了。让我困惑的是我想使用指针,但 googlemock 可以与类一起使用。这是有效的:
这很好,但我想知道如果我真的需要我的指导,如何使这项工作有效。听起来像是给读者的练习。 ;)
I finally got it sorted. What was tripping me up is that I wanted to use a pointer, but googlemock works with classes. Here's what works:
This is well and good, but I wonder how to make this work if I really needed my pointers. Sounds like an exercise for the reader. ;)
怎么样:
}
(免责声明 - 我没有检查它是否编译......)
How about:
}
(Disclaimer - I did not check if it compiles...)