Question

I'm trying to compare two string which are supposed to be the same. The test however fails.

I'm testing if the file get set correctly by the router.setForwarding(true) method.

Here is the code of the test.

void router_test::testSetForwarding_true()
{
    QFile myfile("/proc/sys/net/ipv4/ip_forward");
    myfile.open(QIODevice::ReadOnly | QIODevice::Text);        
    router->setForwarding(true);   
    QString forward = QString(myfile.readAll());

    QCOMPARE(QString("1"),forward);
}

As a result I get:

   FAIL!  : router_test::testSetForwarding_true() Compared values are not the same
   Actual (QString("1")): 1
   Expected (forward): 1

Why aren't they equal?

Was it helpful?

Solution

As you can glean from the output, you have interchanged the actual and expected values. You're also comparing the newline-terminated output against one without a newline.

This should work:

QCOMPARE(forward, QString("1\n"));

or

QCOMPARE(forward[0], QChar('1'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top