【问题标题】:How to test a nested JSON data present in JSON file in Laravel?如何测试 Laravel 中 JSON 文件中存在的嵌套 JSON 数据?
【发布时间】:2021-08-18 13:24:20
【问题描述】:

目前我正在使用它,它不适用于嵌套的 JSON 文件

 public function testitemExists(){
        $response = $this->json('GET','/api/items');
        $response->assertStatus(200)->assertJsonFragment(['category'=>'drinks']);
    }
{
 "category" :"drinks",
  "items":[{
"item1":"mojito",
"item2":"cocacola"
}
]

}

我如何测试 item1 => mojito 是否存在

【问题讨论】:

    标签: php json laravel unit-testing testing


    【解决方案1】:

    试试这个(使用 Laravel 8.x):

        $response
        ->assertOk()
        ->assertJson(
            fn (AssertableJson $json) =>
            $json->has('category')
                ->has(
                    'items',
                    1,
                    fn ($json) =>
                    $json->where('item1', "mojito")
                        ->where('item2', 'cocacola')
                )
        );
    

    这里是official documentation。

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2022-01-16
      • 2017-06-24
      • 2020-06-03
      • 2019-03-13
      相关资源
      最近更新 更多