我正在使用 Qt 的 QTestLib 单元测试框架 + GMOck。
我正在尝试使用 gMock 设置模拟服务器,以便在单元测试中简单地从 QTcpSocket 接收数据到 VerifySendData() 方法。
如果有人能给我一个例子,我如何创建模拟对象 gMOCK(期望调用,创建模拟对象)
#ifndef TST_TCPCOMMTEST_H
#define TST_TCPCOMMTEST_H
#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QTcpSocket>
#include <QTcpServer>
class TcpCommTest : public QObject
{
Q_OBJECT
private:
QTcpSocket* pTestSocket;
QTcpSocket* pMockSocket;
QTcpServer* pMockServer;
public:
TcpCommTest();
public slots:
void connectMockServer();
private Q_SLOTS:
void initTestCase();
void VerifySendData();
void cleanupTestCase();
};
#endif // TST_TCPCOMMTEST_H
#include "tst_tcpcommtest.h"
#include <QtDebug>
#include <QtCore/>
#include <QtTest/QtTest>
#include <QtNetwork/QtNetwork>
#include <gmock/gmock.h>
void TcpCommTest::connectMockServer()
{
cout << "connection attempt" << endl;
pMockSocket = pMockServer->nextPendingConnection();
}
void TcpCommTest::initTestCase()
{
pMockSocket = NULL;
pMockServer = new QTcpServer();
//I want to use gMock here to create MockTCP Server
EXPECT_CALL(pMockServer,someMethod(1).Times(AtLeaset(1));
EXPECT_CALL(pMockServer,someMethod(2).Times(AtLeaset(1));
//pMockServer->listen( QHostAddress("127.0.0.1"), 2000 );
//connect( pMockServer, SIGNAL(newConnection()), SLOT(connectMockServer()) );
pTestSocket = new QTcpSocket();
pTestSocket->connectToHost( QHostAddress("127.0.0.1"), 2000 );
QVERIFY( pTestSocket->waitForConnected( 1000 ) );
QVERIFY( pTestSocket->state() == QTcpSocket::ConnectedState );
}
void TcpCommTest::VerifySendData()
{
int i=0;
QVERIFY( pMockSocket != NULL );
}
QTEST_MAIN(TcpCommTest);