vendredi 23 novembre 2018

Laravel 5.5 testing ModelNotFoundException exception for delete api routes

I'm having odd behaviour in a laravel 5.5 project. I have some feature tests set up and working, and need to test that a specific route will return a 404 if the id passed in doesn't exist. I have explicit model binding set up in the RouteServiceProvider for my Note model

Route::bind('note', function($value){
            return Note::where('id', $value)->first() ?? abort(404);
        });

This works for my get route test. This test below passes as expected. ($this->headers is just some bits I set in a setUp method that is needed for a lot of the tests)

/** @test */
    public function error_received_if_note_does_not_exist()
    {
        $this->withExceptionHandling();

        $response = $this->json('GET', '/api/v1/note/1', [], $this->headers);
        $response->assertStatus(404);
    }

but this one for the delete route fails ...

/**
     * @test
     * @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function error_received_if_note_not_found()
    {
        $this->withExceptionHandling();

        $response = $this->json('DELETE', '/api/v1/note/1', [], $this->headers);
        $response->assertStatus(404);
    }

with message Failed asserting that exception of type "\Illuminate\Database\Eloquent\ModelNotFoundException" is thrown.

I get that technically the exception is correct, but I want to assert that I get a 404 error code.

Here is the routes/api.php file

Route::apiResource('note', 'NoteController')->only([
        'show',
        'destroy'
    ]);

I'm pulling my hair out. Any ideas welcome.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire