実現したいこと
以下のような、バリデーションをFormRequestを使って実装し、テストコードを書いているのですが、実装方法わからず困っています。
テストでやりたいこととしては、バリデーションエラーになった際に、failedValidationで実装したHttpResponseExceptionの例外がスローされて、422のレスポンスがきちんと返ることをテストしたいです。
どなたかアドバイスいただければ幸いです。よろしくお願いいたします。
<?php namespace App\Http\Requests; use Illuminate\Contracts\Validation\Validator; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Exceptions\HttpResponseException; class HogeRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "name" => "string", ]; } public function messages() { return [ "name.string" => "文字列で入力してください。", ]; } /** * @param Validator $validator */ protected function failedValidation(Validator $validator) { throw new HttpResponseException( response()->json($req, 422) ); } }