Remove
$this->withoutExceptionHandling()
Also you are missing redirect
return redirect()->route('profile',['user' => auth()->user()]);
Summer Sale! All accounts are 50% off this week.
Hi - I'm testing to ensure my validations are working as expected. The controller uses a FormRequest for validation. The test function is:
public function test_user_profile_validation_fails_when_first_is_missing()
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
auth()->loginUsingId($user->id);
$response = $this->post('profile/update/'.$user->id,
[
'first' => '',
'last' => 'Hunt',
]);
$response->assertStatus(302)
->assertValidationErrors(['first']);
}
and this is controller's update function:
public function update(ProfileRequest $request, User $user)
{
return route('profile',['user' => auth()->user()]);
}
and the ProfileRequest
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'first' => ['string','required'],
'last' => ['string','required'],
];
}
I expect this test to pass, but I get an error AND the test fails:
Illuminate\Validation\ValidationException : The first must be a string. (and 1 more error)
which is true but frustrates my ability to test the condition. I'm sure I've got something wrong and would appreciate your guidance! Thanks.
Remove
$this->withoutExceptionHandling()
Also you are missing redirect
return redirect()->route('profile',['user' => auth()->user()]);
Please or to participate in this conversation.