4

My test will fail if I use RefreshDatabase trait.

Without the trait -- test will pass but data remains in database.

I'm using:

  • Laravel 5.5 (a fresh one)
  • Laradock: nginx mysql selenium

Tests are being running from a laradock's workspace.

This is the test, simple, from an official documentation.

<?php

namespace Tests\Browser;

use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{
    use RefreshDatabase;

    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testLogin()
    {
        $user = factory(User::class)->create();

        // dump('wait...');
        // sleep(15);
        // dump('go!');

        $this->browse(function (Browser $browser) use ($user) {
            $browser->visit('/login')
                    ->type('email', $user->email)
                    ->type('password', 'secret')
                    ->press('Login')
                    ->assertPathIs('/home')
            ;
        });
    }
}

dump/sleep/dump is just to see if the user has been created in the database. It's not!

The result of php artisan dusk is:

There was 1 failure:

1) Tests\Browser\LoginTest::testLogin
Failed asserting that '/login' matches PCRE pattern "/^\/home/u".

/var/www/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:53
/var/www/tests/Browser/LoginTest.php:32
/var/www/vendor/laravel/dusk/src/TestCase.php:92
/var/www/tests/Browser/LoginTest.php:34

NOTE:
Asserting text "These credentials do not match our records." will be true, so I believe that user has never been created.

3

3 Answers 3

1

In Laravel Dusk document has mentioned:

Most of the tests you write will interact with pages that retrieve data from your application's database; however, your Dusk tests should never use the RefreshDatabase trait. The RefreshDatabase trait leverages database transactions which will not be applicable or available across HTTP requests. Instead, use the DatabaseMigrations trait, which re-migrates the database for each test:

So the answer is using DatabaseMigrations instead of RefreshDatabase

0

Use DatabaseMigrations trait instead of RefreshDatabase trait.

0
0

I don't think that the problem is using sqlite/pgsql/whateversql driver. And I sure you don't need to put something like $app['config']... lines to \Tests\CreatesApplication::createApplication.

I solved this problem running artisan config:clear before artisan serve. It will make new phpunit.dusk.xml which will route to .env.dusk.{env}, and then rerun dusk command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.