PHPUnit returning 404 after first GET request

Posted 1 week ago by pedroroccon

Greetings!

I'm getting an error 404 in my PHPUnit Tests. After doing my first GET Request, every next requests returns a 404 error. To make easier to understand, I made 2 methods with exactly same tests.

The first test returns me success, but the second fails with 404 error. If I try to run the second test alone, it returns me success!

Someone knows how to fix that? Thanks :)

My test class:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Sys\Organizacao;

class OrganizacaoCrudTest extends TestCase
{
    use RefreshDatabase;

    protected $organizacao;

    protected $user;

    public function setUp() : void
    {
        parent::setUp();
        $this->organizacao = factory(Organizacao::class)->create();
        $this->user = factory('App\User')->create();
    }

    /** @test */
    public function has_index()
    {
         // Returns success.
        $this->actingAs($this->user)->get('admin/organizacao')
            ->assertStatus(200);
    }

    /** @test */
    public function has_create_form()
    {
         // Running this test alone, returns success, but when I run the entire class, this method fails (404 error)
        $this->actingAs($this->user)->get('admin/organizacao')
            ->assertStatus(200);
    }
}

My PHPUnit Return

phpunit --filter=OrganizacaoCrudTest

PHPUnit 7.5.13 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 547 ms, Memory: 22.00 MB

There was 1 failure:

1) Tests\Feature\OrganizacaoCrudTest::has_create_form
Expected status code 200 but received 404.
Failed asserting that false is true.

/var/www/caob-app/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:151
/var/www/caob-app/tests/Feature/OrganizacaoCrudTest.php:36

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

Please sign in or create an account to participate in this conversation.