下記コードで日付をcreated_at, updated_atに代入した時に1971-12-13 23:16:32
から1971-12-13T23:16:32.000000Z
に変わってしまっているのはなぜでしょうか?
$expected_data = $designers->map(function ($designer) { return [ 'id' => $designer->id, 'name' => $designer->name, 'taxonomy' => $designer->taxonomy, 'created_at' => $designer->created_at, 'updated_at' => $designer->updated_at ]; })->all();
// ./vendor/bin/phpunit --textdox Feature\DesignerApi ✘ Should 正しい構造のJSONを返却する │ │ Unable to find JSON fragment: │ │ [[{"created_at":"1971-12-13T23:16:32.000000Z","id":1,"name":"Abe Heaney III","taxonomy":"1","updated_at":"1981-08-03T05:06:54.000000Z"}]] │ │ within │ │ [[{"created_at":"1971-12-13 23:16:32","id":1,"name":"Abe Heaney III","taxonomy":"1","updated_at":"1981-08-03 05:06:54"},{"created_at":"1988-11-07 23:52:18","id":5,"name":"Rory Breitenberg","taxonomy":"3","updated_at":"2014-03-30 23:21:58"},{"created_at":"2001-01-17 15:30:48","id":4,"name":"Mandy Kovacek IV","taxonomy":"3","updated_at":"1986-10-14 12:17:57"},{"created_at":"2004-01-22 21:47:16","id":3,"name":"Paris O'Connell","taxonomy":"1","updated_at":"1983-10-13 12:08:16"},{"created_at":"2006-11-21 03:53:56","id":2,"name":"Clotilde Rowe","taxonomy":"3","updated_at":"2017-01-23 03:31:13"}]]. │ Failed asserting that false is true. │ │
コード
php
1// DesignerApiTest.php 2 3namespace Tests\Feature; 4 5use App\User; 6use App\Designer; 7use App\DesignerTaxonomy; 8use Tests\TestCase; 9use Illuminate\Foundation\Testing\WithFaker; 10use Illuminate\Foundation\Testing\RefreshDatabase; 11 12class DesignerApiTest extends TestCase 13{ 14 use RefreshDatabase; 15 16 public function setUp(): void 17 { 18 parent::setUp(); 19 20 $this->user = factory(User::class)->create(); 21 } 22 23 /** 24 * @test 25 */ 26 public function should_正しい構造のJSONを返却する() 27 { 28 // 5つのデザイナーデータを生成する 29 factory(Designer::class, 5)->create(); 30 31 $response = $this->actingAs($this->user) 32 ->json('GET', route('designer.index')); 33 34 $designers = Designer::all(); 35 36 $expected_data = $designers->map(function ($designer) { 37 return [ 38 'id' => $designer->id, 39 'name' => $designer->name, 40 'taxonomy' => $designer->taxonomy, 41 'created_at' => $designer->created_at, 42 'updated_at' => $designer->updated_at 43 ]; 44 })->all(); 45 46 $response->assertStatus(200) 47 ->assertJsonCount(5); 48 $response->assertJsonFragment($expected_data); 49 50 } 51 52}
php
1// DesignerFactory.php 2 3use App\Designer; 4use Illuminate\Support\Str; 5use Faker\Generator as Faker; 6 7$factory->define(Designer::class, function (Faker $faker) { 8 return [ 9 'name' => $faker->name, 10 'taxonomy' => rand(0, 3), 11 'created_at' => $faker->dateTime(), 12 'updated_at' => $faker->dateTIme(), 13 ]; 14});
php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Designer; 6use App\DesignerTaxonomy; 7use App\Http\Requests\CreateDesigner; 8use Illuminate\Http\Request; 9use Illuminate\Support\Facades\Auth; 10use Illuminate\Support\Facades\DB; 11 12class DesignerController extends Controller 13{ 14 protected $hidden = [ 15 'created_at', 'updated_at' 16 ]; 17 18 public function __construct() 19 { 20 // 認証が必要 21 $this->middleware('auth'); 22 } 23 24 public function index() 25 { 26 $designers = Designer::all(); 27 28 return response($designers, 200); 29 } 30 31} 32
回答1件
あなたの回答
tips
プレビュー