私が書いた Qt5 アプリケーションの単体テストを作成しようとしていますが、ネットワークに関連するクラスを処理する方法について困惑しています。私のメイン クラスには、QTcpServer::incomingConnection をオーバーライドして ClientConnection オブジェクトを作成し、それをスレッドに渡す QTcpServer サブクラスが含まれています。
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();
}
ClientConnection クラスは、socketDescriptor を使用して専用スレッドで新しい QTcpSocket を開き、クライアントからデータを受信して処理します。
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();
}
このクラスはまだ終わっていませんが、今からテストを書き始めたいと思っています。私の問題は、socketDescriptor の処理方法です。ある種の依存性注入を使用する必要があると思いますが、テスト ケースで QTcpServer 全体を作成しないと実現できないと思います。
最近はネットワーク コードのテストが一般的になっているはずなので、アプリケーションの半分を含めずにこれを処理する一般的な方法が必要です。これは一般的な質問のように思えますが、私の特定のアプリケーションに関する詳細が必要な場合はお知らせください。