前提・実現したいこと
LaravelでPHPUnitを利用して単体テストを実装中。
storeの単体テストで仮データ(factoryを利用)を作成して実施するも下記エラーが発生する。
factoryを利用して作成したダミーデータは配列型の認識なのですが、代入できずエラーとなっている認識です。
こちらの解決策あればご指摘いただければと思います。
発生している問題・エラーメッセージ
There was 1 error: 1) Tests\Feature\UserTest::testStore TypeError: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::post() must be of the type array, object given, called in /var/www/html/tests/Feature/UserTest.php on line 49 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:315 /var/www/html/tests/Feature/UserTest.php:49 --
該当のソースコード
php
1//UserTest.php 2<?php 3 4namespace Tests\Feature; 5 6use App\User; 7use Illuminate\Foundation\Testing\DatabaseTransactions; 8use Tests\TestCase; 9 10class UserTest extends TestCase 11{ 12 /** 13 * A basic feature test example. 14 * 15 * @return void 16 */ 17 18 19 use DatabaseTransactions; 20 21 public function testIndex() { 22 $this->withoutExceptionHandling(); 23 $response = $this -> withoutMiddleware() ->get('/api/users'); 24 $response ->assertStatus(200); 25 26 } 27 28 public function testShow() { 29 $this->withoutExceptionHandling(); 30 31 $response = $this -> withoutMiddleware() -> get("/api/users/1"); 32 $response -> assertStatus(200); 33 } 34 35 public function testStore() { 36 $this->withoutExceptionHandling(); 37 38 $dummy = factory(User::class)->create([ 39 "name" => "testman", 40 "email" => "test@gmail.com", 41 "password" => "p@ssw0rd", 42 ]); 43 44 $this->assertDatabaseHas("users",[ 45 "name" => "testman", 46 "email" => "test@gmail.com", 47 "password" => "p@ssw0rd", 48 ]); 49 50 //エラー該当箇所 51 $response = $this -> withoutMiddleware() -> post("/api/users",$dummy); 52 $response -> assertStatus(201); 53 } 54 55 public function testUpdate() { 56 $this->withoutExceptionHandling(); 57 58 $dummyNameInfo = ['name' => 'ユーザ名前変更']; 59 $response = $this->withoutMiddleware() -> patch("/api/users/1",$dummyNameInfo); 60 $response->assertStatus(204); 61 } 62 63 public function testDestroy() { 64 $this->withoutExceptionHandling(); 65 $response = $this ->withoutMiddleware()->delete("api/users/1"); 66 $response ->assertStatus(204); 67 } 68}
//UserFactory.php <?php /** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\User; use Faker\Generator as Faker; use Illuminate\Support\Str; /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | This directory should contain each of the model factory definitions for | your application. Factories provide a convenient way to generate new | model instances for testing / seeding your application's database. | */ $factory->define(User::class, function (Faker $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; });
//UserController.php <?php namespace App\Http\Controllers\Api; use App\Hobby; use App\Http\Controllers\Controller; use App\Story; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use App\User; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { //一覧取得 $users = User::all(); return response()->json(['users' => $users], 200); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // ユーザー登録 // 仮登録 $pre_authority = 1; $user = User::create([ 'name' => 'nkruser', 'authority' => $pre_authority, 'email' => $request['email'], // 'email' => hash('sha256', $user_info['email']), // 'email' => Crypt::encryptString($user_info['email']), 'password' => Hash::make($request['password']), ]); return response()->json($user, 201); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { //ユーザー詳細取得 $user = User::where('id', $id) ->with([ 'hobbies', 'stories', 'videos', 'works', 'academics', 'skills', 'qualifications' ])->first(); return response()->json($user, 200); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // $user = User::where('id', $id)->first(); if ($request->password && $request->email) { $pass = Hash::make($request->password); $user->fill(array_merge( $request->all(), [ 'password' => $pass, 'email' => $request['email'] // 'email' => hash('sha256', $request['email']) // 'email' => Crypt::encryptString($user['email']), ] ))->save(); return response()->json($user, 204); } else if ($request->email) { $user->fill(array_merge( $request->all(), [ // 'email' => hash('sha256', $request['email']) 'email' => $request['email'] ] ))->save(); return response()->json($user, 204); } else if ($request->password) { $pass = Hash::make($request->password); $user->fill(array_merge( $request->all(), [ 'password' => $pass, ] ))->save(); return response()->json($user, 204); } else { $user->fill(array_merge( $request->all(), ))->save(); return response()->json($user, 204); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // $user = User::find($id); $user->delete(); return response()->json(['message' => 'delete successfully'], 204); } }
試したこと
- Qiitaなどの参考文献を調査してみるも解決できず
- stack overflowの英語文献をみるも、PHPUnitで同じエラーとなっているものは存在せず。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。