Laravel Dusk:如何使用内存数据库进行测试 [英] Laravel Dusk: how to use in-memory DB for testing

查看:107
本文介绍了Laravel Dusk:如何使用内存数据库进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在使用 Laravel Dusk 进行测试时使用内存数据库.

What I've been trying is to use in-memory database while testing with Laravel Dusk.

这里有一个文件.env.dusk.local,具有以下值.

Here we have a file, .env.dusk.local, with the following values.

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

这是浏览器测试文件的摘要.

Here is a snippet of a browser testing file.

class ViewOrderTest extends DuskTestCase
{
    use DatabaseMigrations;

    /** @test */
    public function user_can_view_their_order()
    {
        $order = factory(Order::class)->create();

        $this->browse(function (Browser $browser) use ($order) {
            $browser->visit('/orders/' . $order->id);
            $browser->assertSee('Order ABC'); //Order name
        });
    }
}

执行php artisan dusk后,Dusk开始浏览器测试.

When php artisan dusk is executed, Dusk starts browser testing.

但是,Dusk似乎正在访问我的本地数据库,因为测试浏览器上有一个订单名称,该订单名称仅存在于我的本地数据库中,而"Order ABC"预计会显示在浏览器中.

However, Dusk seems to be accessing my local DB, because there is an order name on the testing browser which only exists in my local DB, while 'Order ABC' is expected to be displayed on the browser.

根据文档,Laravel Dusk允许我们设置环境变量.

According to the doc, Laravel Dusk allows us to set the environmental variables.

要在运行测试时强制Dusk使用其自己的环境文件,请在项目的根目录中创建一个.env.dusk.{environment}文件.例如,如果要在本地环境中启动dusk命令,则应创建一个.env.dusk.local文件.

To force Dusk to use its own environment file when running tests, create a .env.dusk.{environment} file in the root of your project. For example, if you will be initiating the dusk command from your local environment, you should create a .env.dusk.local file.

我不觉得Dusk正在访问单独的数据库.

I don't feel that Dusk is accessing the seperate DB.

任何建议将不胜感激.

推荐答案

在Laravel黄昏浏览器测试期间,您不能使用:memory:数据库.您的开发服务器和黄昏测试在单独的进程上运行.灰尘测试无法访问在开发服务器上运行的进程的内存.

You can't use :memory: database while Laravel dusk browser testing. Your development server and dusk testing runs on separate processes. dust test cannot access to memory of process running on development server.

创建sqlite文件数据库进行测试的最佳解决方案.

Best solution it to create sqlite file database for testing.

'sqlite_testing' => [
      'driver'   => 'sqlite',
      'database' => database_path('sqlite.testing.database'),
      'prefix'   => '',
 ],

在数据库文件夹中创建sqlite.testing.database文件.

Create sqlite.testing.database file inside database folder.

在使用

php artisan serve --env dusk.local

这篇关于Laravel Dusk:如何使用内存数据库进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆