目前我正在使用这个,它不适用于嵌套 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 是否存在
最佳答案
试试这个(使用 Laravel 8.x):
$response
->assertOk()
->assertJson(
fn (AssertableJson $json) =>
$json->has('category')
->has(
'items',
1,
fn ($json) =>
$json->where('item1', "mojito")
->where('item2', 'cocacola')
)
);
关于php - 如何在 Laravel 中测试 JSON 文件中存在的嵌套 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67770324/