Laravel dusk not working .env.dusk.local

2020-08-17 18:22发布

问题:

I have an application and I want to use Laravel Dusk.

I created a file named .env.dusk.local with a database for tests and a file named .env with my default database.

I ran the php artisan command and created a user by /register.

After I created a login test with the same email but with a different password, this would not be a problem because in .env.dusk.local it would be a different bank and would not have any users registered.

But when I run the php artisan dusk command it takes the information from the original .env and ends up erasing all records from my default database.

I would like to know how to load the information from my .env.dusk.local and use my test database.

.env default

APP_ENV=local
APP_KEY=base64:K8otIkxJk0rFsZYSEq1hwBuaxQX3QI3Bb7ZmemJRIWg=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk
DB_USERNAME=root
DB_PASSWORD=123456

.env.dusk.local

APP_ENV=local
APP_KEY=base64:K8otIkxJk0rFsZYSEq1hwBuaxQX3QI3Bb7ZmemJRIWg=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk_test
DB_USERNAME=root
DB_PASSWORD=123456

Mu function for testLogin

namespace Tests\Browser;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\App;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{


    use DatabaseMigrations;
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testLogin()
    {
        $user = factory(\App\User::class)->create(['email' => 'lucas@example.com']);

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

Here is this project in github

回答1:

Instead of .env.dusk.local try .env.dusk

Also instead of using your mysql database I would recommend using a temporary sqlite db since it gets created and destroyed during tests.

you will have to have a sqilte config in your database.php that points at an actual .sqlite file that you have in your installation

so copy the sqlite config in database.php and then paste it, name it sqlite_dusk maybe, then for the location of the db put it as storage_path('dusk.sqlite') or something like that. Then create a blank dusk.sqlite file in the root of your storage folder.

Then in your .env.dusk set:

DB_CONNECTION=sqlite_dusk

Hope that helps!