首页 > 解决方案 > PHPUnit - 尝试测试 json 数组中的键和值

问题描述

我有以下 Json 数组:

[
    {
        "ticket_id": 131,
        "ticket_subject": "quasi",
        "ticket_content": "However, everything is queer to-day.' Just then she walked up towards it rather timidly, saying to.",
        "ticket_name": "Prof. Ellen Yost Jr.",
        "ticket_email": "sosinski@example.org",
        "ticket_status": 0,
        "created_at": "2021-04-19 16:53:33",
        "updated_at": "2021-04-19 16:53:33"
    }
]

我正在尝试编写一个功能测试来检查它是否具有 'ticket_status' 键和0.

到目前为止,在我的测试文件中,我有以下代码:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class OpenTicketTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testOpenTickets()
    {
        $this->get(route('tickets.open'))
            ->assertStatus(200)
            ->assertJsonStructure([
                '*' => [
                    'ticket_id',
                    'ticket_subject',
                    'ticket_content',
                    'ticket_name',
                    'ticket_email',
                    'ticket_status',
                    'created_at',
                    'updated_at'
                ]
            ])->assertJson([
                'ticket_status' => 0
            ]);
    }
}

但是当我运行测试时,我在控制台中收到以下错误消息:

Unable to find JSON:

[{
    "ticket_status": 0
}]

within response JSON:

[[]].


Failed asserting that an array has the subset Array &0 (
    'ticket_status' => 0
).
--- Expected
+++ Actual
@@ @@
 array (
-  'ticket_status' => 0,
 )

任何人对使用正确的语法来检索所需结果有任何想法吗?

谢谢,

罗伯特

标签: phpjsonlaravelphpunit

解决方案


推荐阅读