laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.33k stars 10.96k forks source link

PHPUnit Test can't resolve route() for first time when using RefreshDatabase #23014

Closed frdteknikelektro closed 6 years ago

frdteknikelektro commented 6 years ago

Description:

I running test but always failed at first route() function, but after that is working, here all test consist route(), but the first one always fail.

Runtime:       PHP 7.2.1-1+ubuntu16.04.1+deb.sury.org+1
Configuration: /home/farid/Code/Web/jala-web/phpunit.xml

"Class Farm\StockController does not exist"
F..............................................................  63 / 113 ( 55%)
..................................................              113 / 113 (100%)

Time: 29.33 seconds, Memory: 34.00MB

There was 1 failure:

1) Tests\Feature\Farm\StockControllerTest::testIndex
The response is not a view.

/home/farid/Code/Web/jala-web/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:613

/home/farid/Code/Web/jala-web/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:538
/home/farid/Code/Web/jala-web/tests/Feature/Farm/StockControllerTest.php:31

FAILURES!
Tests: 113, Assertions: 161, Failures: 1.

Steps To Reproduce:

On tests/Feature/ExampleTest.php edit to became

    public function testBasicTest()
    {
        $response = $this->get(route('home', [], false));
        dump($response->exception->getMessage());

        $response->assertStatus(200);
    }

Then run

vendor/bin/phpunit tests/Feature/ExampleTest.php

I know this should be failed of unauthenticated, but it failed on different case. This is happen when I use RefreshDatabase Trait on TestCase Class.

frdteknikelektro commented 6 years ago

How is exactly best practice to migrate and seed database before test? not every test.

frdteknikelektro commented 6 years ago

After more investigations, the problem is happens because I override refreshTestDatabase() to run command migrate:fresh --seed, if I remove the --seed, it works. Is this intended?

    protected function refreshTestDatabase()
    {
        if (! RefreshDatabaseState::$migrated) {
            $this->artisan('migrate:fresh', [ '--seed' => true ]);

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        }

        $this->beginDatabaseTransaction();
    }
frdteknikelektro commented 6 years ago

Aaand the problem is happen because of I install voyager package. and there were some strange line of code on seeder on MenuItemsTableSeeder.php#L17. If I comment this line, no issue happens

Then I'm going to close this.