assertJson()用于Laravel中的JSON数组

时间:2019-11-27 07:33:28

标签: php json laravel rest unit-testing

我正在从此页面为Laravel重新创建一个简单的api: https://www.toptal.com/laravel/restful-laravel-api-tutorial?utm_campaign=blog_post_restful_laravel_api_tutorial&utm_medium=email&utm_source=blog_subscribers

在测试阶段,我正在testArticlesAreListedCorrectly()的{​​{1}}函数中遇到麻烦。该代码在页面中并不完美,就像我必须将ArticleTest.php更改为jsonAssert()才能使测试正常进行。但是在响应中声明多个Json数组似乎很困难,并且显示错误。有没有办法在两个数组同时使用JsonFragment的同时检查它们是否正确列出?

jsonAssertFragment()

错误:

public function testArticlesAreListedCorrectly()
{

    factory(Article::class)->create([
        'title' => 'First Article',
        'body' => 'First Body'
    ]);

    factory(Article::class)->create([
        'title' => 'Second Article',
        'body' => 'Second Body'
    ]);

    $user = factory(User::class)->create();
    $token = $user->generateToken();
    $headers = ['Authorization' => "Bearer $token"];

    $response = $this->json('GET','/api/articles', [], $headers)
        ->assertStatus(200)
        ->assertJson([
            ['title' => 'First Article', 'body' => 'First Body'],
            ['title' => 'Second Article', 'body' => 'Second Body']
        ])
        ->assertJsonStructure([
            '*' => ['id', 'body', 'title', 'created_at', 'updated_at'],
        ]);
}

0 个答案:

没有答案