如何使用Laravel检查JSON片段中是否存在该值

时间:2018-11-01 14:04:51

标签: laravel unit-testing

我正在尝试使用Laravel测试方法测试我的api,我想测试资源是否仅返回“星球大战”类别中的帖子。我正在使用assertJsonFragment(),但始终失败,并显示消息“无法找到JSON片段”

api的响应:

{
   "data":[
      {
         "id":1,
         "subject":"C3PO",
         "body":"test",
         "thumbnail":"",
         "date":"2018-10-11 12:08:27",
         "category":{
            "id":7,
            "name":"Star Wars",
            "tag":"star_wars",
            "icon":"star_wars",
            "color":"005374"
         }
      },
      {
         "id":2,
         "subject":"R2-D2",
         "body":"test",
         "thumbnail":"",
         "date":"2018-10-03 14:42:07",
         "category":{
            "id":7,
            "name":"Star Wars",
            "tag":"star_wars",
            "icon":"star_wars",
            "color":"005374"
         }
      }
   ]
}

我基本上尝试了该尝试的所有变化,

$response->assertStatus(200)
        ->assertJsonFragment([
            'data' => [
                'category' => [
                    'name' => "Star Wars"
                ]
            ]
                             ])
        ->assertJsonStructure(
            [
                'data' => [
                    'id',
                    'subject',
                    'thumbnail',
                    'date',
                    'category' => [
                        'id',
                        'name',
                        'tag',
                        'icon',
                        'color',
                    ],
                ],
            ]
        );

有人可以帮助我吗?

0 个答案:

没有答案