assertJsonStringEqualsJsonString()


assertJsonStringEqualsJsonString(mixed $expectedJson, mixed $actualJson[, string $message = ''])

$actualJson 对应的值与 $expectedJson 对应的值不匹配时报告错误,错误讯息由 $message 指定。


例 A.35: assertJsonStringEqualsJsonString() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3.  
  4. class JsonStringEqualsJsonStringTest extends TestCase
  5. {
  6. public function testFailure()
  7. {
  8. $this->assertJsonStringEqualsJsonString(
  9. json_encode(['Mascot' => 'Tux']),
  10. json_encode(['Mascot' => 'ux'])
  11. );
  12. }
  13. }
  14. ?>
  1. phpunit JsonStringEqualsJsonStringTest
  2. PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
  3.  
  4. F
  5.  
  6. Time: 0 seconds, Memory: 5.00Mb
  7.  
  8. There was 1 failure:
  9.  
  10. 1) JsonStringEqualsJsonStringTest::testFailure
  11. Failed asserting that two objects are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. stdClass Object (
  16. - 'Mascot' => 'Tux'
  17. + 'Mascot' => 'ux'
  18. )
  19.  
  20. /home/sb/JsonStringEqualsJsonStringTest.php:5
  21.  
  22. FAILURES!
  23. Tests: 1, Assertions: 3, Failures: 1.