Q&A
Laravel でテストを実行してます
User とBordは一対多でリレーションしてます。
api でユーザーデータを21件つくって、pagenation(20)でちゃんと帰ってきているかテスト
実現したいこと
下記の状態をステータスPASSにしたい
Unexpected properties were found on the root level. Failed asserting that two arrays are identical.
該当のソースコード
Controller index メソッド
php
1 2 public function index(User $user) 3 { 4 return Bord::where('user_id', $user->id)->paginate(20); 5 }
BordTest
php
1<?php 2 3namespace Tests\Feature; 4 5use App\Models\Bord; 6use App\Models\User; 7use Illuminate\Foundation\Testing\RefreshDatabase; 8use Illuminate\Foundation\Testing\WithFaker; 9use Illuminate\Testing\Fluent\AssertableJson; 10use Tests\TestCase; 11 12class BordTest extends TestCase 13{ 14 /** 15 * A basic feature test example. 16 * 17 * @return void 18 */ 19 20 use RefreshDatabase; 21 22 public function setUp(): void 23 { 24 parent::setUp(); 25 26 $user = User::factory()->create(['id' => 1]); 27 Bord::factory()->count(21)->for($user)->create(); 28 } 29 public function test_5_items_in_one_page() 30 { 31 $response = $this->get('/api/v1/users/1/bords'); 32 $response->assertJson( 33 fn (AssertableJson $json) => 34 $json->has('data', 20) 35 ); 36 } 37} 38
現状
自分に理解としては、Bord::factory()->count(21)の部分で21件のデータを作り、
$json->has('data', 20) の部分で帰って来たデータが20件あるかを判定してる、と理解しているのでこのテストがPASS にならない理由がわからないです。
自分の理解が間違っているのか、記法がちがうのかわからないのでご教示いただきたいです。よろしくお願いいたします。
Laravel 8
回答1件
良いと思った回答にはグッドを送りましょう。
グッドが多くついた回答ほどページの上位に表示されるので、他の人が素晴らしい回答を見つけやすくなります。
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/11/13 15:09