How to create .env file for test with Laravel Dusk

时间秒杀一切 提交于 2019-12-23 08:57:35

问题


I'm using Dusk to do a simple login test.

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

Archive .env

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 it deletes all data from the database used in the application.

How can I solve this problem?


回答1:


You have to append an environment value, (which matches the environment you'll be initializing Dusk in), to the end of your .env.dusk file name, (e.g. - .env.dusk.local). For reference check the documentation on Dusk Environment Handling.

Update: If you're still having issues per your comments, put the following at the top of your testLogin function and report back what it says dd(env('APP_ENV'));




回答2:


@alaric

I changed the .env.dusk.testing file to .env.dusk.local

I ran the php artisan serve and created a new user in the laravel_dusk database.

I ran the php artisan serve again and then the php artisan dusk to run the tests and create a new user with the same email but in the database laravel_dusk_test and it continues registering in laravel_dusk.




回答3:


Instead of using a 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!




回答4:


I had similar issue when i was using \App\User::truncate() in my dusk file.

It truncates the dev database instead of the test database. I don't know exactly why because php artisan dusk replace the .env file with the .env.dusk.local for the test. Adding the whole configuration of the DB in the .env.dusk.local did the job for me.

So first, if your file .env have APP_ENV=local then rename your dusk file to .env.dusk.local

Second, in your .env.dusk.local be sure to use the whole configuration of your database:

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

instead of

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

then run your test with

php arisan dusk


来源:https://stackoverflow.com/questions/43046749/how-to-create-env-file-for-test-with-laravel-dusk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!