4

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

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

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

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.

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

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.

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

Any advice will be appreciated.

1
  • I don't think it's possible to use in-memory SQLite along Dusk. Commented Feb 15, 2018 at 15:02

2 Answers 2

15

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.

Best solution it to create sqlite file database for testing.

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

Create sqlite.testing.database file inside database folder.

Make sure to run development server before running tests using

php artisan serve --env dusk.local
Sign up to request clarification or add additional context in comments.

1 Comment

I like this answer but I'm curious about performance of :memory: versus SQLite versus MySQL. Since we can't do :memory: with Dusk will it be a lot more performant to use SQLite instead of MySQL?
-1

You need a connection in config/database.php

'sqlite_testing' => [
     'driver'   => 'sqlite',
     'database' => ':memory:',
     'prefix'   => '',
],

Then in your phpunit.xml file use:

<env name="DB_DEFAULT" value="sqlite_testing" />

or in your tests use:

 putenv('DB_DEFAULT=sqlite_testing');

Don't forget to use the RefreshDatabase trait to reset the database before each test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.