How to create .env file for test with Laravel Dusk

I use Dusk for a simple login test.

I created the .env.dusk file so that the test uses an alternative database and does not delete the data registered on the platform.

.Env archive

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

Archive .env.dusk

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

LoginTest.php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

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

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

But when I run the tests, it does not change the database and deletes all the data from the database used in the application.

How can I solve this problem?

+6
source share
4 answers

( , Dusk), .env.dusk(, -.env.dusk.local). Dusk Environment Handling.

. , testLogin , dd(env('APP_ENV'));

+4

@alaric

.env.dusk.testing .env.dusk.local

php artisan serve laravel_dusk.

php artisan serve, php artisan dusk, , laravel_dusk_test laravel_dusk.

+1

mysql sqlite, .

sqilte database.php, .sqlite, .

sqlite database.php, , sqlite_dusk, , db storage_path('dusk.sqlite') - . dusk.sqlite storage.

.env.dusk :

DB_CONNECTION=sqlite_dusk

, !

0

, \App\User :: truncate() .

dev . , , php artisan dusk .env .env.dusk.local . .env.dusk.local .

, -, .env APP_ENV = local, dusk .env.dusk.

-, .env.dusk.local :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tdd_test
DB_USERNAME=your_username
DB_PASSWORD=your_password

DB_CONNECTION=test // configuration of test in the /config/database.php

php arisan dusk
0

Source: https://habr.com/ru/post/1016072/


All Articles