我的應用程式中有以下測驗。但是回應沒有通過測驗,我不明白為什么......
public function test_get()
{
$user = $this->signInAsUser();
$product = factory('App\Models\Product')->create();
factory('App\Models\Stock')->create([
'product_id' => $product->id,
'qty' => $qty = rand(1, 100),
]);
$this->json('GET', '/api/stock', , ['Accept' => 'application/json'])
->assertStatus(200)
->assertJson([
'data' => [
'product_id' => "$product->id",
'stock' => "$qty"
]
]);
}
這會從 PHPUnit 產生以下內容:
Unable to find JSON:
[{
"data": {
"product_id": "1",
"stock": "55"
}
}]
within response JSON:
[{
"data": [
{
"product_id": "1",
"stock": "55"
}
]
}].
Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'product_id' => '1'
'stock' => '55'
)
).
斷言未通過測驗,我不明白為什么 JSON 對我來說看起來一樣......
uj5u.com熱心網友回復:
您斷言 JSON 回應是單個物件,而它是一個物件陣列。嘗試使用assertJsonFragment而不是assertJson.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386363.html
上一篇:Lumen-在測驗中使用factory()函式會出錯-未定義的函式
下一篇:使用字典對檔案進行搜索和替換
