您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertJsonStringEqualsJsonFile
发布时间:2021-10-04 20:56:44编辑:雪饮阅读()
assertJsonStringEqualsJsonFile断言用来断言一个实际值(’json数据/json字符串’)与一个期望值(’文件内容/json字符串’),若是相同,则断言成功。
JsonStringEqualsJsonFileTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class JsonStringEqualsJsonFileTest extends TestCase
{
public function testFailure(): void
{
$this->assertJsonStringEqualsJsonFile('path/to/fixture/file', json_encode(['Mascot' => 'ux']));
}
public function testSuccess(): void
{
$this->assertJsonStringEqualsJsonFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt', json_encode(['Mascot' => 'ux']));
}
}
参与的文件D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt:
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt
{"Mascot":"ux"}
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\JsonStringEqualsJsonFileTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.009, Memory: 6.00 MB
There was 1 failure:
1) JsonStringEqualsJsonFileTest::testFailure
Failed asserting that file "path/to/fixture/file" exists.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\JsonStringEqualsJsonFileTest.php:8
FAILURES!
Tests: 2, Assertions: 5, Failures: 1.
关键字词:phpunit,assertJsonStringEqualsJsonFile