Laravel 测试:如果需要检查多个级别,assertJsonFragment 会失败
Posted
技术标签:
【中文标题】Laravel 测试:如果需要检查多个级别,assertJsonFragment 会失败【英文标题】:Laravel Testing: assertJsonFragment fails if multiple levels need to be checked 【发布时间】:2018-11-07 19:56:17 【问题描述】:这是回复:
[
"data":
"locales":
"translate":[
"created_at":"2018-05-28 12:49:53",
"deleted_at":null,
"id":1,
"key":"nl_NL",
"name":"Netherlands (Nederlands)",
"updated_at":"2018-05-28 12:49:53"
],
"validate":[
"created_at":"2018-05-28 12:49:53",
"deleted_at":null,
"id":2,
"key":"it_IT",
"name":"Italian (Italiano)",
"updated_at":"2018-05-28 12:49:53"
]
,
"error":false,
"message":null
]
我想断言以下片段是响应的一部分:
1) ['translate' => [['key' => 'nl_NL']]]
2) ['validate' => [['key' => 'it_IT']]]
有没有办法断言translate
数组至少包含一个键为nl_NL
的元素,而validate
包含一个键为it_IT
的元素?
$response->assertSuccessful()->assertJsonFragment([
'translate' => [['key' => 'nl_NL']],
'validate' => [['key' => 'it_IT']
]);
【问题讨论】:
【参考方案1】:assertJsonFragment()
无法做到这一点。
你可以这样做:
$this->assertTrue(
collect($response->decodeResponseJson('0.data.locales.translate'))
->pluck('key')->contains('nl_NL')
);
【讨论】:
以上是关于Laravel 测试:如果需要检查多个级别,assertJsonFragment 会失败的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 5 Eloquent在多个级别上向JSON添加关系