我来自 phpunit 的 assertEquals 说我的字符串不相等,但我已经复制了它们,我认为这是由于行被添加到响应中
My assertEquals from phpunit says my strings are not equal but I have copied them and I think it is due to lines being added to the response
我正在尝试 运行 一些测试,使用 guzzle 提示我的服务器进行 JSON 响应,我会将其与我期望的响应进行比较。问题是我无法得到与预期结果相匹配的响应。我认为这主要是由于一些语法错误,但我不知道如何纠正它。
我已经尝试在输出中手动添加新行,但这只是将 \n 添加到字符串中。
请参阅下面的控制台报告:
1) UserAgentTest::testGet
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"error":false,"user":{"id_customer":10,"name":"Nic","email":"nparmee@gmail.com","password":"y$YN5A58syg5iIuHJ.5BR6j.ZpHGASqZID4l6k.M3xgH9jSPlojhzGK","x_coord":"-29.82188!-29.79044","y_coord":"31.02221!30.80688","address_name":"Home!Not home","address":"11 Rapson Road!11 everton road","country":null,"province":"KwaZulu-Natal!KwaZulu-Natal","city":"Durban!Durban","suburb":"Morningside!Kloof","postcode":"2639!3620","account":"sole","vat_num":"","reg_id":"","phone_number":"0836661065"}}'
+'
+
+{"error":false,"user":{"id_customer":10,"name":"Nic","email":"nparmee@gmail.com","password":"y$YN5A58syg5iIuHJ.5BR6j.ZpHGASqZID4l6k.M3xgH9jSPlojhzGK","x_coord":"-29.82188!-29.79044","y_coord":"31.02221!30.80688","address_name":"Home!Not home","address":"11 Rapson Road!11 everton road","country":null,"province":"KwaZulu-Natal!KwaZulu-Natal","city":"Durban!Durban","suburb":"Morningside!Kloof","postcode":"2639!3620","account":"sole","vat_num":"","reg_id":"","phone_number":"0836661065"}}'
C:\wamp64\www\tests\UserAgentTest.php:32
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
"actual" 字符串中有 2 个 前导 换行符:
+'
+
+{"error":false
您可以使用 PHP 的 trim
函数去除字符串开头和结尾的空格。
self::assertEquals(trim($string1), trim($string2));
这将确保没有 leading/trailing 个空白字符。
此外,您可能希望使用 preg_replace
:
去除所有换行符
self::assertEquals(
trim(preg_replace('/\R+/', '', $string1)),
trim(preg_replace('/\R+/', '', $string2))
);
我正在尝试 运行 一些测试,使用 guzzle 提示我的服务器进行 JSON 响应,我会将其与我期望的响应进行比较。问题是我无法得到与预期结果相匹配的响应。我认为这主要是由于一些语法错误,但我不知道如何纠正它。
我已经尝试在输出中手动添加新行,但这只是将 \n 添加到字符串中。
请参阅下面的控制台报告:
1) UserAgentTest::testGet
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"error":false,"user":{"id_customer":10,"name":"Nic","email":"nparmee@gmail.com","password":"y$YN5A58syg5iIuHJ.5BR6j.ZpHGASqZID4l6k.M3xgH9jSPlojhzGK","x_coord":"-29.82188!-29.79044","y_coord":"31.02221!30.80688","address_name":"Home!Not home","address":"11 Rapson Road!11 everton road","country":null,"province":"KwaZulu-Natal!KwaZulu-Natal","city":"Durban!Durban","suburb":"Morningside!Kloof","postcode":"2639!3620","account":"sole","vat_num":"","reg_id":"","phone_number":"0836661065"}}'
+'
+
+{"error":false,"user":{"id_customer":10,"name":"Nic","email":"nparmee@gmail.com","password":"y$YN5A58syg5iIuHJ.5BR6j.ZpHGASqZID4l6k.M3xgH9jSPlojhzGK","x_coord":"-29.82188!-29.79044","y_coord":"31.02221!30.80688","address_name":"Home!Not home","address":"11 Rapson Road!11 everton road","country":null,"province":"KwaZulu-Natal!KwaZulu-Natal","city":"Durban!Durban","suburb":"Morningside!Kloof","postcode":"2639!3620","account":"sole","vat_num":"","reg_id":"","phone_number":"0836661065"}}'
C:\wamp64\www\tests\UserAgentTest.php:32
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
"actual" 字符串中有 2 个 前导 换行符:
+'
+
+{"error":false
您可以使用 PHP 的 trim
函数去除字符串开头和结尾的空格。
self::assertEquals(trim($string1), trim($string2));
这将确保没有 leading/trailing 个空白字符。
此外,您可能希望使用 preg_replace
:
self::assertEquals(
trim(preg_replace('/\R+/', '', $string1)),
trim(preg_replace('/\R+/', '', $string2))
);