前提・実現したいこと
Laravel内のphpunitを利用して単体テストを実装中。
認証処理についてはLaravelの初期にあるものをいじっていない状態です。
投稿主の認識ですとemail
とpassword
があれば通る認識ですが通らず。
Laravel初学者であるため、こちらの原因についてご指摘いただけると幸いです。
発生している問題・エラーメッセージ
Illuminate\Validation\ValidationException: The given data was invalid.
該当のソースコード
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = RouteServiceProvider::HOME; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } /** * The user has been authenticated. * * @param \Illuminate\Http\Request $request * @param mixed $user * @return mixed */ protected function authenticated(Request $request, $user) { return $user; } }
//AuthTest.php <?php namespace Tests\Feature; use App\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\WithoutMiddleware; class AuthTest extends TestCase { /** * A basic feature test example. * * @return void */ use DatabaseTransactions; use WithoutMiddleware; public function testLogin() { $this->withoutExceptionHandling(); $user = factory(User::class)->create([ "email" =>"test@gmail.com", "password" => "p@ssw0rd0" ]); $response = $this->post('/api/login',["email" =>"test@gmail.com","password" => "p@ssw0rd0"]); $response->assertStatus(200); } // public function testLogout() // { // $response = $this->get('/'); // $response->assertStatus(200); // } }
<?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' => 'p@ssw0rd', // password 'remember_token' => Str::random(10), ]; });
試したこと
- postmanで既存ユーザでログインするようAPIを叩いてみると処理は成功する
- phpunitのテスト内で既存ユーザ情報をパラメータで渡しても同じエラーが出る
- qiitaで似た文献を参照するも解決できず
あなたの回答
tips
プレビュー