JSON响应断言失败PHP单元

时间:2017-05-23 20:01:40

标签: 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"
  }
 ]
}

然后我创建了一个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-&gt; 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,所以我知道响应是准确的。< / p>

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

您的回复数据的格式为HashMap,您想要的子集位于{ 'data' => [ ... ] }内。但是,您要求查找结构[ ... ],它在顶层不存在。您需要在{ 'id': ..., ... }

的数据成员中声明JSON

您可以改为使用:

$response