When I use Form Request and test my submissions, I often end up with Illuminate\Validation\ValidationException: The given data was invalid. without any hint which data is wrong or missing. Disabling exception handling does not improve the error message.
I tried to output the session errors in the test with dd($request->session()->all() but the exception breaks the test before that already.
How can I get the exact validation error(s) in my test?
Here's a basic setup to see what I mean:
// TestFormRequest
public function rules()
{
return [
'size' => 'required',
'department' => 'required',
];
}
// Controller
public function store(TestFormRequest $request)
{
Company::store($request->validated());
}
// Test the happy path, but has an error (size ommited on purpose to test exception message)
/** @test */
public function test() {
$this->withoutExceptionHandling();
$response = $this->actingAs($user)->post(route('company.store'), [
'department' => 'Admin',
// 'size' ommited on purpose to simulate the exception error message
]);
$response->assertRedirect(route('home'));
}
I would expect an exception along the following lines:
Illuminate\Validation\ValidationException: The given data was invalid. Size is missing.
Instead I get no details on my validation error, just a generic exception:
# Error Output when running test
Illuminate\Validation\ValidationException: The given data was invalid.
│
╵ /Users/test/Sites/test/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:130
╵ /Users/test/Sites/test/vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php:26
╵ # REDACTED TO IMPROVE READABILITY
╵ /Users/test/Sites/test/tests/Feature/Strike
╵ /Users/test/Sites/test/tests/Feature/StrikeTest.php:148