I'm trying to write unit tests for a Qt5 application that I wrote, but I'm stumped about how to handle the classes that involve networking. My main class includes a QTcpServer subclass, which overrides QTcpServer::incomingConnection to create a ClientConnection object and hands it off to a thread:
void NetworkServer::incomingConnection(qintptr socketDescriptor)
{
QThread* clientThread = new QThread();
ClientConnection* clientConnection = new ClientConnection(socketDescriptor);
clientConnection->moveToThread(clientThread);
// connect signals & slots to ClientConnection (removed for clarity)
connect(clientThread, &QThread::started, clientConnection, &ClientConnection::run);
clientThread->start();
}
The ClientConnection class uses the socketDescriptor to open a new QTcpSocket in the dedicated thread, receives data from a client, and processes it.
ClientConnection::ClientConnection(int socketDescriptor, QObject *parent) :
QObject(parent), socketDescriptor(socketDescriptor)
{
tcpIncomingData = new QByteArray;
}
void ClientConnection::run()
{
QTcpSocket socket;
if(!socket.setSocketDescriptor(socketDescriptor)) {
emit sig_error(socket.error());
return;
}
if(socket.waitForReadyRead(5000)) {
*tcpIncomingData = socket.readAll();
qDebug() << "data received: " << tcpIncomingData;
} else {
qDebug() << "socket timed out!";
}
parseXmlData();
socket.disconnectFromHost();
socket.waitForDisconnected();
}
This class isn't finished but I want to start writing tests now. My issue is how to handle the socketDescriptor. I assume I need to use some sort of dependency injection, but I don't think that's feasible without creating an entire QTcpServer in the test case.
Testing network code must be common these days, so there must be a common way to handle this without including half of my application. This seems like a general question, but if more detail about my specific application is required please let me know.