Sareneathenodny's avatar

Dusk unexpectedly accessing two databases in same test

My dusk test is accessing two different databases.

I'd like it to only access one.

Any help would be appreciated!

Details:

I have two databases: one for testing (test_db) and one for production (prod_db).

My .env file specifies DB_DATABASE=prod_db.

My .env.dusk.local file specifies DB_DATABASE=test_db.

I call php artisan dusk --env=dusk.local for this test.

When my test calls factory(User::class)->create(), the record is entered into the test_db.

But when the same test then calls $browser->visit('/login') and types and submits email and password, it is searching prod_db.

Any ideas what might be going on?

My test class:

<?php

namespace Tests\Browser;

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

class ExampleTest extends DuskTestCase
{
    use DatabaseMigrations;

    public function testBasicExample()
    {
        $this->browse(function (Browser $browser) {

            $user = factory(User::class)->create([
                'name' => 'Amy Ames',
                'email' => '[email protected]',
                'password' => \Hash::make('password')
            ]);        

            // THIS PASSES
            $this->assertDatabaseHas('users', ['email' => $user->email]);
            
            // THIS PRINTS "test_db", AS SPECIFIED IN .env.dusk.local
            echo "\n" . env('DB_DATABASE') . "\n";            
            
            // THIS FAILS WATING FOR LOCATION.
            // ERROR MESSAGE ON SCREENSHOT = 
            //     "These credentials do not match our records."
//     $browser->visit('/login')
//              ->type('email', '[email protected]')
//              ->type('password', 'password')
//              ->click('@login-submit')
//              ->waitForLocation('/dashboard')
//              ->assertSee('Amy Ames')
//              ->assertSee("Dashboard works!");

            // THIS PASSES, BUT THE USER John Doe IS IN MY "prod_db" DATABASE, AS 
            // SPECIFIED IN .env
            $browser->visit('/login')
                ->type('email', '[email protected]')
                ->type('password', 'password')
                ->click('@login-submit')
                ->waitForLocation('/dashboard')
                ->assertSee('John Doe')
                ->assertSee("Dashboard works!");

        });
    }
}
0 likes
5 replies
shez1983's avatar

i think this makes sense because your test at that point is VIEWING your site as if you would.. and when you do, your data doesnt end up in the test db rather in prod db.. you are not supposed to use dusk for production.. only your local/staging.. and for that i think this is fine..

Sareneathenodny's avatar

Thanks @shez1983 . I'm still not sure I've got my head around it 100%, but I've set everything to "testing" environment in all .env* files, and set all .env* files to access the same db during local development, and it's a setup that works effectively for me for now.

shez1983's avatar

was there a q in your last post? :p BTW dusk does something silly it copies your .env to .env.testing and in my past experience this can fail or something, cant remember exactly

staudenmeir's avatar

Are you using Dusk in combination with php artisan serve?

shez1983's avatar

i used it with homestead iirc (last year) not using it currently

Please or to participate in this conversation.