gpt4 book ai didi

php - JSON 响应断言失败 PHP 单元

转载 作者:行者123 更新时间:2023-12-02 17:19:13 29 4
gpt4 key购买 nike

我正在创建一个 Laravel 应用程序并使用 PHP Unit 进行测试。我正在尝试为我的端点编写测试。我有一个端点,它返回数据库中所有客户端的 JSON 响应,如下所示:

{
data: [
{
id: 1,
name: "test",
password: "p@ssword",
description: "test client",
ip: "192.168.0.1",
created: "2017-05-18T19:16:20+00:00",
updated: "2017-05-18T19:16:23+00:00"
},
{
id: 2,
name: "test",
password: "p@ssword",
description: "test client 2",
ip: "192.168.0.2",
created: "2017-05-22T19:16:20+00:00",
updated: "2017-05-22T19:16:23+00:00"
}
]
}

然后我创建了一个 ClientControllerTest,它将使用测试数据库和 Faker 检查 JSON 响应。我的ClientControllerTest如下:

<?php

namespace Tests\App\Http\Controllers;

use Tests\TestCase;
use Carbon\Carbon;
use App\Client;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class ClientsControllerTest extends TestCase
{
use DatabaseMigrations;

public function setUp()
{
parent::setUp();
Carbon::setTestNow(Carbon::now('UTC'));
}

public function tearDown()
{
parent::tearDown();
Carbon::setTestNow();
}

/** @test */
public function index_status_code_should_be_200()
{
$this->get('api/clients')->assertStatus(200);
}

/** @test */
public function index_should_return_a_collection_of_records()
{
$clients = factory(Client::class, 2)->create();

$response = $this->json('GET', 'api/clients');

$response->assertStatus(200);

$content = json_decode($response->getContent(), true);
$this->assertArrayHasKey('data', $content);

foreach ($clients as $client) {
$response->assertJson([
"id" => $client->id,
"name" => $client->name,
"password" => $client->password,
"description" => $client->description,
"ip" => $client->ip,
"created" => $client->created_at->toIso8601String(),
"updated" => $client->updated_at->toIso8601String()
]);
}
}
}

然而,当我尝试运行 PHPUnit 时,我收到一个奇怪的错误,它似乎断言 $response->assertJSON 失败,即使我可以在错误消息中看到输出?报错信息如下:

1) Tests\App\Http\Controllers\ClientsControllerTest::index_should_return_a_collection_of_records
Unable to find JSON:

[{
"id": 1,
"name": "Christophe Sporer",
"password": "VaK~\\g",
"description": "Saepe nisi accusamus numquam dolores voluptate id.",
"ip": "195.177.237.184",
"created": "2017-05-23T19:53:43+00:00",
"updated": "2017-05-23T19:53:43+00:00"
}]

within response JSON:

[{
"data": [
{
"id": 1,
"name": "Christophe Sporer",
"password": "VaK~\\g",
"description": "Saepe nisi accusamus numquam dolores voluptate id.",
"ip": "195.177.237.184",
"created": "2017-05-23T19:53:43+00:00",
"updated": "2017-05-23T19:53:43+00:00"
},
{
"id": 2,
"name": "Miss Virginie Johnson",
"password": "e1\"~q\\*oSe^",
"description": "Nihil earum quia praesentium iste nihil nulla qui occaecati non et perspiciatis.",
"ip": "110.125.57.83",
"created": "2017-05-23T19:53:43+00:00",
"updated": "2017-05-23T19:53:43+00:00"
}
]
}].


Failed asserting that an array has the subset Array &0 (
'id' => 1
'name' => 'Christophe Sporer'
'password' => 'VaK~\g'
'description' => 'Saepe nisi accusamus numquam dolores voluptate id.'
'ip' => '195.177.237.184'
'created' => '2017-05-23T19:53:43+00:00'
'updated' => '2017-05-23T19:53:43+00:00'
).

我有点困惑为什么这会失败,因为 PHPUnit 似乎没有提供更多信息,而且我确实断言对 api/clients 的请求有效并返回了 200,所以我知道响应是准确的。

感谢任何帮助,谢谢!

最佳答案

您的响应数据的格式为 { 'data' => [ ... ] },其中您想要的子集在 [ ... ] 中。但是,您要求查找结构 { 'id': ..., ... },它在顶层不存在。您需要在 $response 的数据成员中断言 JSON。

您可以改用:

assertJson([ 'data' => [ "id" => $client->id, ... ] ])

关于php - JSON 响应断言失败 PHP 单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44144030/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com