我正在使用Qt的QTestLib單元測試框架+ GMOck。QTestLib單元測試框架+ Gmock(創建QTCPServer - 模擬對象)
我想設置模擬服務器使用gMock簡單地接收數據從一個單元測試QTcpSocket VerifySendData()方法。
如果有人給我一個例子,我怎麼可以創建模擬對象gMOCK(除CALL,創建Mock對象)
#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);