BlackFox
BlackFox

Reputation: 21

Laravel Expected status code 200 but received 500

That's the code for my unit test, the default one:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

I'm running the laravel project on: php artisan serv

When I manually access the / the project works fine! however on the test I'm getting this error:

  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

Here it is the full output:

  FAIL  Tests\Feature\ExampleTest
  ⨯ basic test

  ---

  • Tests\Feature\ExampleTest > basic test
  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

  at tests/Feature/ExampleTest.php:19
     15▕     public function testBasicTest()
     16▕     {
     17▕         $response = $this->get('/');
     18▕ 
  ➜  19▕         $response->assertStatus(200);
     20▕     }
     21▕ }
     22▕

How can I fix that please?

Upvotes: 2

Views: 3435

Answers (2)

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

in my case:

i added env variable that forget to add to my .env.testing

by this comment: @jamesclark

It seems like there is something wrong with your testing environment which is causing an error

ican understand and add it to my .env file and all test run as well

Upvotes: 0

Riccardo
Riccardo

Reputation: 46

Before your $this->get() call, insert a call to $this->withoutExceptionHandling(). It will show you the detail of the error.

Upvotes: 3

Related Questions