如何创建.env文件用于Laravel evening的测试

时间:2022-06-08 12:07:00

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.

我创建了一个.env。黄昏文件,以便测试使用一个备用数据库,并且不删除在平台上注册的数据。

Archive .env

归档.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

归档.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

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?

我如何解决这个问题?

3 个解决方案

#1


3  

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.

您必须在.env的末尾附加一个环境值(它与您将要初始化的黄昏环境相匹配)。黄昏文件名,例如。env.dusk.local。为了参考,请检查黄昏环境处理的文档。

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'));

更新:如果您的评论仍然有问题,请将以下内容放在testLogin函数的顶部,并报告它说的dd(env('APP_ENV'));

#2


1  

@alaric

@alaric

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

我改变了.env.dusk。测试文件.env.dusk.local

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

我运行了php artisan服务并在laravel_evening数据库中创建了一个新用户。

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.

我再次运行php artisan服务,然后php artisan黄昏运行测试并创建一个新用户,使用相同的电子邮件但是在数据库laravel_dusk_test中,它继续在laravel_evening中注册。

#3


0  

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

我建议使用临时的sqlite db,而不是使用mysql数据库,因为它在测试期间被创建和销毁。

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

在数据库中必须有一个sqilte配置。指向安装中实际的.sqlite文件的php

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.

复制数据库中的sqlite配置。然后粘贴,命名为sqlite_evening,然后对于db的位置,将它命名为storage_path('dusk.sqlite')或者类似的。然后创造一个空白的黄昏。存储文件夹根目录中的sqlite文件。

Then in your .env.dusk set:

然后在你的.env。黄昏设置:

DB_CONNECTION=sqlite_dusk

Hope that helps!

希望会有帮助!

#1


3  

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.

您必须在.env的末尾附加一个环境值(它与您将要初始化的黄昏环境相匹配)。黄昏文件名,例如。env.dusk.local。为了参考,请检查黄昏环境处理的文档。

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'));

更新:如果您的评论仍然有问题,请将以下内容放在testLogin函数的顶部,并报告它说的dd(env('APP_ENV'));

#2


1  

@alaric

@alaric

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

我改变了.env.dusk。测试文件.env.dusk.local

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

我运行了php artisan服务并在laravel_evening数据库中创建了一个新用户。

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.

我再次运行php artisan服务,然后php artisan黄昏运行测试并创建一个新用户,使用相同的电子邮件但是在数据库laravel_dusk_test中,它继续在laravel_evening中注册。

#3


0  

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

我建议使用临时的sqlite db,而不是使用mysql数据库,因为它在测试期间被创建和销毁。

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

在数据库中必须有一个sqilte配置。指向安装中实际的.sqlite文件的php

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.

复制数据库中的sqlite配置。然后粘贴,命名为sqlite_evening,然后对于db的位置,将它命名为storage_path('dusk.sqlite')或者类似的。然后创造一个空白的黄昏。存储文件夹根目录中的sqlite文件。

Then in your .env.dusk set:

然后在你的.env。黄昏设置:

DB_CONNECTION=sqlite_dusk

Hope that helps!

希望会有帮助!