Summer Sale! All accounts are 50% off this week.

dam-man's avatar

Laravel Unit Tests not working

I am trying to make tests, but they aren't working at all :(

Steps I followed:

  1. php artisan make:test ExampleTest
  2. php artisan config:clear
  3. php artisan test

Example test was created successfully:

namespace Tests\Feature;

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


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

        $response->assertStatus(200);
    }
}

Running the test doesn't result in a proper result

# php artisan test
Warning: TTY mode is not supported on Windows platform.

   FAIL  Tests\Feature\ExampleTest
  ⨯ example

  ---

  • Tests\Feature\ExampleTest > example
   Error

  Call to a member function getStatusCode() on bool

  at C:\wamp64\www\jvz-api\tests\Feature\ExampleTest.php:20
     16▕     public function test_example()
     17▕     {
     18▕         $response = $this->get('/');
     19▕
  ➜  20▕         $response->assertStatus(200);
     21▕     }
     22▕ }
     23▕

  1   C:\wamp64\www\jvz-api\vendor\phpunit\phpunit\phpunit:92
      PHPUnit\TextUI\Command::main()


  Tests:  1 failed
  Time:   0.28s

I have read a lot of tutorials and it should work out of the box but I can't figure out why it is not working. I am trying to connect an API server.

0 likes
11 replies
automica's avatar

do you get a response if you hit that url with postman or load it in your browser?

tykus's avatar

Did you override get somewhere in the class hierarchy?

dam-man's avatar

This is the result when I connect with PHP Storm http request:

###

GET http://back.jvz/
Accept: */*
Cache-Control: no-cache
Content-Type: application/json
HTTP/1.1 200 OK
Date: Fri, 03 Sep 2021 08:49:51 GMT
Server: Apache/2.4.39 (Win64) PHP/7.4.8
Vary: Authorization
X-Powered-By: PHP/7.4.8
Content-Length: 18
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

you shall not pass

Response code: 200 (OK); Time: 245ms; Content length: 18 bytes
AndrykVP's avatar

As someone who just recently had issues with Tests, here are two things you can try:

  • Dump the request with dd($request) before asserting the status, it'll give you an idea of what the route is returning.
  • Use the withoutExceptionHandling() method before making the request, it will also give you insight if anything is messing up with the request. Like so:
$response = $this->withoutExceptionHandling()->get('/');
  • If you're making tests for API endpoints, try using getJson() instead of get(). I honestly don't know if it makes any significant difference, but it's recommended for a reason.
1 like
dam-man's avatar

It doesn't give a clear sight on what is going wrong in the request.

Illuminate\Testing\TestResponse^ {#208
  +baseResponse: false
  #exceptions: Illuminate\Testing\LoggedExceptionCollection^ {#167
    #items: []
  }
  #streamedContent: null
}
tykus's avatar

Can you read further into the stacktrace (or post it here) to see where the getStatus method is being called?

dam-man's avatar

This is the full response of the call :(

Sidra8's avatar

Hi! please help How to change the value of a column named "isVerfied" in the database from 1 to 0 when the user logout in laravel 7?

Please or to participate in this conversation.