Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajsmith_codes's avatar

Laravel 6 - TDD App - Expected status code 403 but received 200.

I am working through Build A Laravel App with TDD and I've been stuck on this particular test.

In the file ManageProjectsTest, there is a test called Authenticated user cannot view projects of others.

This is the code I have:

$this->signIn();

    $project = factory('App\Project')->create();

    $this->get($project->path())->assertStatus(403);

It's the same as the code from the file they provide. However, I keep getting:

Expected status code 403 but received 200. Failed asserting that false is true.

I think the error must be somewhere else, but no luck finding it. Any ideas?

0 likes
7 replies
ajsmith_codes's avatar

I tried adding that line of code and received: This action is unauthorized.

ajsmith_codes's avatar

@thedejavunl Yes, I have that line added. Unfortunately, it doesn't give me much more in the way of explanation. I'm still very new to this. :)

thedejavunl's avatar

What are you doing in the controller, how did you implement the authorization? Can you share the code for this?

jrdavidson's avatar

@ajsmith_codes You'll need to add either one of these two options. The reason you are getting that is that you have the route you are testing has a dependency of an authorized signed-in user. So you will need to create a user and log them in before access the route.

$this->actingAs($user = factory(User::class)->create())->get(...

OR

$this->be($user = factory(User::class)->create())->get(...
ajsmith_codes's avatar
ajsmith_codes
OP
Best Answer
Level 5

Thanks, everyone! I had missed adding $this->signIn() to that particular test. After that, it worked.

Please or to participate in this conversation.