[英]PHPUnit - trying to test for a key and a value from a json array
我有以下 Json 數組:
[
{
"ticket_id": 131,
"ticket_subject": "quasi",
"ticket_content": "However, everything is queer to-day.' Just then she walked up towards it rather timidly, saying to.",
"ticket_name": "Prof. Ellen Yost Jr.",
"ticket_email": "sosinski@example.org",
"ticket_status": 0,
"created_at": "2021-04-19 16:53:33",
"updated_at": "2021-04-19 16:53:33"
}
]
我正在嘗試編寫一個功能測試來檢查它是否具有 'ticket_status' 鍵和相應的值0
。
到目前為止,在我的測試文件中,我有以下代碼:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class OpenTicketTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testOpenTickets()
{
$this->get(route('tickets.open'))
->assertStatus(200)
->assertJsonStructure([
'*' => [
'ticket_id',
'ticket_subject',
'ticket_content',
'ticket_name',
'ticket_email',
'ticket_status',
'created_at',
'updated_at'
]
])->assertJson([
'ticket_status' => 0
]);
}
}
但是當我運行測試時,我在控制台中收到以下錯誤消息:
Unable to find JSON:
[{
"ticket_status": 0
}]
within response JSON:
[[]].
Failed asserting that an array has the subset Array &0 (
'ticket_status' => 0
).
--- Expected
+++ Actual
@@ @@
array (
- 'ticket_status' => 0,
)
任何人對使用正確的語法來檢索所需結果有任何想法嗎?
謝謝,
羅伯特
您的測試中沒有返回數據。 你可能想檢查一下原因。 此外,您可能希望在嘗試 assertJson() 時指定確切的 JSON 結構
$this->get(route('tickets.open'))
->assertStatus(200)
->assertJsonStructure([
'*' => [
'ticket_id',
'ticket_subject',
'ticket_content',
'ticket_name',
'ticket_email',
'ticket_status',
'created_at',
'updated_at'
]
])->assertJson([
// assuming this is the json structure
'data' => [
0 => 'ticket_status' => 0 // if ticket_status is 0 in the first element
]
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.