martinbean's avatar

Dusk configuration; I’m doing something wrong

It’s the weekend. I shouldn’t be working, but I am!

I’m trying to get some Dusk tests running, but having issues with environments.

I’ve read the documentation and created a .env.dusk.local file with the following:

DB_CONNECTION=dusk

I’ve added a dusk connection to my config/database.php file:

'dusk' => [
    'driver' => 'sqlite',
    'database' => database_path('dusk.sqlite'),
    'prefix' => '',
],

In my test, I’ve imported and using the DatabaseMigrations trait. This is what one of my test cases looks like:

public function testUserWithActiveRentalCanWatchVideo()
{
    $video = $this->createPublishedVideo();

    $user = factory(User::class)->create();

    $rental = factory(Rental::class)->states('active')->create([
        'user_id' => $user->getKey(),
        'video_id' => $video->getKey(),
    ]);

    $this->browse(function (Browser $browser) use ($video, $user) {
        $url = route('channel.video.show', [
            $video->channel,
            $video,
        ]);

        $browser->loginAs($user)
                ->visit($url)
                ->assertVisible('video');
    });
}

It seems to create the records in the right database (the SQLite database at database/dusk.sqlite) but when Dusk invokes my application, my application seems to still be using the actual database connection (sqlite). I tested this by adding die(env('DB_CONNECTION')) to the top of my public/index.php file and reviewing the error screen-shot, and it says sqlite rather than the expected dusk.

How do I make Dusk use the database connection configured in my .env.dusk.local file when running tests? From the documentation, it’s my understand Dusk would back up my actual .env file, replace it with .env.dusk.local, and then put my .env file back after Dusk finished running. Is there something I’ve missed, or something else I need to do?

0 likes
9 replies
ohffs's avatar

Might not be the issue - but I usually use a '.env.dusk' file (no '.local' at the end). Also - you can't use the RefreshDatabase trait - you need to use the DatabaseMigrations one. Because your test is now separate from your actual 'app' (as the browser is different process) it'll get very messy.

staudenmeir's avatar

Are you running Dusk with php artisan dusk?

Are you using a phpunit.dusk.xml file?

Put sleep(30); in the test and run it. Does Dusk rename to existing .env to .env.backup?

martinbean's avatar

you can't use the RefreshDatabase trait - you need to use the DatabaseMigrations one.

@ohffs Apologies! I am in fact using the DatabaseMigrations trait and not RefreshDatabase (typing on auto-pilot). I’ve updated my post.

Are you running Dusk with php artisan dusk?

@staudenmeir Yup.

Are you using a phpunit.dusk.xml file?

No.

Does Dusk rename to existing .env to .env.backup?

It does. If I open .env in my text editor, I see its contents get replaced with the contents of .env.dusk.local, a file called .env.backup show up in the file tree, and then revert when the test finishes.

martinbean's avatar

@staudenmeir It’s fine. I’ve just installed Homestead and running my application through that, so my Dusk tests are running as expected now. I just forgot the environment was cached when running an application with artisan serve.

Please or to participate in this conversation.