如何使用Laravel Dusk创建.env文件进行测试 [英] How to create .env file for test with Laravel Dusk
问题描述
我正在使用Dusk进行简单的登录测试.
I'm using Dusk to do a simple login test.
我创建了一个.env.dusk文件,以便测试使用备用数据库,并且不会删除平台上注册的数据.
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
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk
DB_USERNAME=root
DB_PASSWORD=123456
存档.env.dusk
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
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?
推荐答案
您必须在.env.dusk文件名的末尾附加一个环境值(与您将初始化Dusk的环境匹配). ,(例如-.env.dusk.local).作为参考,请查看黄昏环境处理上的文档.
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.
更新:如果您的评论仍有问题,请将以下内容放在testLogin函数的顶部,并报告其内容dd(env('APP_ENV'));
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'));
这篇关于如何使用Laravel Dusk创建.env文件进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!