我目前正在为我的一个 class 编写一些单元测试。但是,我很快遇到了一个问题。似乎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()信号,是否有人对此有任何解释?

谢谢!

最佳答案

最后,问题在于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);
}

关于qt - QSignalSpy无法捕获信号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53385144/

10-11 16:52