zoransa's avatar

Is this right way to test request and guard

Hi guys!

Let's say everything is implemented like in Taylor's tutorial

https://laravel.com/docs/5.2/quickstart-intermediate#deleting-tasks

If I would like to test lets say $user1 makes a $task, $user2 try to submit delete request and my test shows green but I wonder did I do it right?

/** @test */
    public function user_cannot_delete_somebody_else_task()
    {
        $user1 = factory(App\User::class)->create();
        $user2 = factory(App\User::class)->create();

        $task = $user1->tasks()->save(factory(App\Task::class)->create());

        $response = $this->actingAs($user2)
            ->call(
                'POST',
                '/task/'.$task->id,
                [
                    '_token' => csrf_token(),
                    '_method' => 'DELETE'
                ]
            );

        $this->assertEquals(403, $response->status());

    }

0 likes
2 replies
bobbybouwmann's avatar
Level 88

You can simply check that by running this assersion

// Check if it exists in the database, do this before delete
$this->seeInDatabase('tasks', ['id' => $task->id]);

// Check if it DOES NOT exists in the database, do this after delete
$this->dontSeeInDatabase('tasks', ['id' => $task->id]);

Source: https://laravel.com/docs/5.2/testing#working-with-databases

zoransa's avatar

Yes only in this case we should see record in database before and after the request.

Please or to participate in this conversation.