2017-05-23 16 views
1

私はLaravelアプリケーションを作成しており、PHPユニットをテスト用に使用しています。私は私のエンドポイントのテストを書くことを試みています。私はそうのようなデータベース内のすべてのクライアントのJSONレスポンスを返すエンドポイントを持っている:JSONレスポンスアサートがPHPユニットに失敗する

{ 
data: [ 
{ 
    id: 1, 
    name: "test", 
    password: "[email protected]", 
    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: "[email protected]", 
    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" 
    } 
] 
} 

私は、JSONレスポンスは、テストデータベースと偽物を使用していることを確認しますClientControllerTestを作成しました。次のように私のClientControllerTestは次のとおりです。私はPHPUnitのを実行しようとすると

<?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() 
      ]); 
     } 
    } 
} 

は、しかし、私は$対応 - > 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に/クライアントが働いたと返されたことを主張しなかったとして失敗している理由として少し混乱しています200なので、回答は正確です。

すべてのヘルプは高く評価され、感謝!

答えて

1

応答データのフォーマットは{ 'data' => [ ... ] }で、希望するサブセットは[ ... ]の内部にあります。しかし、トップレベルには存在しない構造{ 'id': ..., ... }を探しています。 $responseのデータメンバー内にJSONをアサートする必要があります。あなたが代わりに使用することができます

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

だけ追記として、あなたはまた、Laravel 5.4のようassertJsonFragmentを使用することができます – liamjnorman

関連する問題