Corbin's avatar

How do I make sure $this->validate asserts 422 in Laravel 8?

This code worked perfectly fine in Laravel 7. There must've been a change to $this-validate() in Laravel 8. How do I make sure I get a 422?

UniverseTest

public function a_universe_requires_a_name()
{   
    $this->withoutExceptionHandling();

    $user = User::factory()->create();

    $this->actingAs($user);

    $universe = Universe::factory()->make(['user_id' => $user->id, 'name' => '']);

    $response = $this->json('POST',  '/api/universe/', $universe->toArray())
        ->assertStatus(422);
}

UniverseController

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'color' => 'required'
    ]);
    
    $universe = Universe::create([
        'name' => $request->input('name'),
        'color' => $request->input('color'),
        'user_id' => Auth::user()->id
    ]);

    return new UniverseResource($universe);
}
0 likes
2 replies
Corbin's avatar

This was a me issue:

Get rid of $this->withoutExceptionHandling();

tisuchi's avatar

@corbin Is that still failing after removing $this->withoutExceptionHandling()?

Please or to participate in this conversation.