JSON 响应断言失败 PHP 单元

lia*_*man 2 php json unit-testing laravel-5

我正在创建一个 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"
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个 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()
            ]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行 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'
).
Run Code Online (Sandbox Code Playgroud)

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

任何帮助表示赞赏,谢谢!

bis*_*hop 6

您的响应数据具有格式{ 'data' => [ ... ] },您想要的子集位于[ ... ]. 但是,您要求找到{ 'id': ..., ... }在顶层不存在的结构。您需要在 的数据成员中声明 JSON $response

您可以改为使用:

assertJson([ 'data' => [ "id" => $client->id, ... ] ])
Run Code Online (Sandbox Code Playgroud)

  • 作为旁注,您还可以从 Laravel 5.4 开始使用 assertJsonFragment (2认同)