我目前正在为我的一个课程编写一些单元测试。但是,我很快遇到了一个问题。似乎QSignalSpy有时无法捕获我的disconnected()
类发出的Client
信号。这是我的单元测试:
void ClientTest::test_disconnectFromHost()
{
QTcpServer server;
server.listen(QHostAddress::Any, 65000);
QTcpSocket* clientSocket = new QTcpSocket();
QSignalSpy connectedSpy(clientSocket, SIGNAL(connected()));
clientSocket->connectToHost(QHostAddress::Any, 65000);
if (!connectedSpy.wait()) {
QFAIL("Client socket connection failed.");
}
Chat::Client client(clientSocket);
QSignalSpy disconnectedSpy(clientSocket, SIGNAL(disconnected()));
// Return true if disconnection succeded
QVERIFY(client.disconnectFromHost());
QVERIFY(disconnectedSpy.wait(500)); // RETURNS FALSE!
// Return false if client was not connected
QVERIFY(!client.disconnectFromHost());
QCOMPARE(client.getLastError(),
Chat::Client::Error::NotConnected);
clientSocket->connectToHost(QHostAddress::Any, 65000);
if (!connectedSpy.wait()) {
QFAIL("Client socket connection failed.");
}
QTcpSocket* serverSocket = server.nextPendingConnection();
serverSocket->disconnectFromHost(); // RETURNS FALSE!
// Emit disconnected if the server closed the connection
QVERIFY(disconnectedSpy.wait(500));
delete serverSocket;
}
这是相关的类方法:
Client::Client(QTcpSocket* socket, QObject* parent)
: QObject(parent)
{
this->socket = socket;
messageSize = -1;
lastError = None;
lastErrorString = "";
connect(socket, SIGNAL(connected()), this, SLOT(onSocketConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
}
bool Client::connectToHost(const QHostAddress& serverAddress, quint16 serverPort)
{
if (socket->state() != QTcpSocket::SocketState::UnconnectedState) {
lastError = AlreadyConnected;
return false;
}
messageSize = -1;
socket->connectToHost(serverAddress, serverPort);
return true;
}
bool Client::disconnectFromHost()
{
if (socket->state() != QTcpSocket::SocketState::ConnectedState) {
lastError = NotConnected;
return false;
}
socket->disconnectFromHost();
return true;
}
void Client::onSocketConnected()
{
emit connected();
}
void Client::onSocketDisconnected()
{
emit disconnected();
}
[...]
有人知道为什么QSignalSpy.wait()方法返回false,即使通过调试单元测试我知道发出了disconnected()
信号吗?
谢谢!
答案 0 :(得分:2)
最后,问题在于QSignalSpy将在调用QSignalSpy :: wait()之前捕获发出的信号。因此,QSignalSpy :: wait()总是导致超时并返回false。
我通过实现两个辅助方法来验证QSignalSpy的计数,然后等待该计数不正确,来解决此问题。然后,我在单元测试中使用这些方法。
namespace SignalUtils {
bool verifySignal(QSignalSpy& spy, int expectedCount);
bool verifyNoSignal(QSignalSpy& spy, int expectedCount);
}
bool SignalUtils::verifySignal(QSignalSpy& spy, int expectedCount)
{
return spy.count() == expectedCount || spy.wait(1000);
}
bool SignalUtils::verifyNoSignal(QSignalSpy& spy, int expectedCount)
{
return spy.count() == expectedCount && !spy.wait(1000);
}