QtTest在比较字符串时失败了,即使它们是相同的?C++

Ang*_*uck 1 c++ qt qttest

我有一个比较两个字符串值的QtTest:

 // This definitely returns "5/6" 
 std::string actual_value = simple(actual);

 // Sets a string to the expected value
 std::string expected_value = "5/6";
Run Code Online (Sandbox Code Playgroud)

然后是QCOMPARE:

QCOMPARE(QString(actual_value.c_str()), QString(expected_value.c_str()));

/* I'm Aware this could just be the code below (but this doesnt print values to
console so i can check they are actually the same)  */
QCOMPARE(actual_value, expected_value)
Run Code Online (Sandbox Code Playgroud)

我得到了错误:(生病后两者所以很明显它不仅仅是QString失败了,所以下面是使用QString显示值的QCOMPARE)

FAIL!  : TestFraction::testFraction() Compared values are not the same
Actual   (QString(actual_value.c_str())): 5/6

Expected (QString(expected_value.c_str())): 5/6
Run Code Online (Sandbox Code Playgroud)

这是正常的QCOMPARE错误消息:

FAIL!  : TestFraction::testFraction() Compared values are not the same
Run Code Online (Sandbox Code Playgroud)

显然还有其他代码,但我现在不发布它,因为我不认为它特别相关,但问你是否想知道你想知道的东西,只要我能看到这个测试应该通过但也许我不知道有关比较字符串的东西(比较如何比较双打​​失败,除非你使用模糊比较)

提前致谢

Vai*_*sai 6

您的实际字符串有一个换行符.因此比较失败了.