GithubHelp home page GithubHelp logo

Comments (14)

MarceauKa avatar MarceauKa commented on May 3, 2024 20

Solved by editing tests/CreatesApplication.php:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();
    $app['config']->set('database.default','sqlite');
    $app['config']->set('database.connections.sqlite.database', ':memory:');

    return $app;
}

from dusk.

splatEric avatar splatEric commented on May 3, 2024 3

A bit more on this, since I just spent 40 minutes wrestling with getting my tests working again. I'm not sure entirely what changed, but I was again having an issue with the consistency of the db connection used by the testing process versus that of the web server process.

There has been an update to dusk to 1.0.4 that forces a reload of the environment in the testing process after the environment files are moved around, however the reload calls Dotenv()->overload(), which means that if any environment variables are not set in your dusk env file, they will default to whatever you had in your core .env file. This was causing a problem with the database filename I was using for sqlite.

TLDR; I set up an sqlite_testing connection in my database config, and pointed at that from the dusk .env file:

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

This seems to have resolved my connection consistency issues.

from dusk.

avrahamappel avatar avrahamappel commented on May 3, 2024 2

I think the issue is setting the environment to local, because then the server uses the values in .env which also has the environment set to local. When I created an .env.dusk.testing file with the following values in it, everything worked fine. No need for a new php artisan serve.

APP_ENV=testing
...

DB_CONNECTION=sqlite

from dusk.

splatEric avatar splatEric commented on May 3, 2024 1

You won't be able to use an in memory db because, as Taylor has pointed out, the tests and the web server are running under separate processes. You could use a common mysql db connection or a single sqlite file, and then do a migrate/refresh option (although this does rely on all your migrations supporting down migration steps).

The approach I have taken (which is adapted from an approach I was using pre-dusk) is to create an empty file for an sqlite db that both my web server and my tests point to. The tests create this file as part of the setup process, and migrate it, so you have a clean db at the start of each test, Much like you get with an in memory sqlite db.

If you want to see an example, take a look at my medium post that has the Trait I wrote for this:

https://medium.com/@splatEric/working-with-laravel-dusk-54d67cc0241b#.xqalwcjxi

from dusk.

MarceauKa avatar MarceauKa commented on May 3, 2024

Same here!

from dusk.

mstnorris avatar mstnorris commented on May 3, 2024

Taylor replied "I don't think that's gonna work. Dusk runs in a separate process", but I don't see how this makes a difference.

from dusk.

mstnorris avatar mstnorris commented on May 3, 2024

@MarceauKa this does not work for me, the sample LoginTest fails with a message saying the credentials do not match. Does this mean that the database was migrated?

from dusk.

mstnorris avatar mstnorris commented on May 3, 2024

Thanks, I'll check it out.

from dusk.

iveoles avatar iveoles commented on May 3, 2024

Is this the same for the DatabaseTransactions trait?

If I use it I run into errors with the credentials not being correct when logging in. If I disable it the test works, but then fails the next time because the user is still in the database.

Do we need to write Dusk tests to clean up after themselves? This is on a MySQL database that's used for local development.

from dusk.

splatEric avatar splatEric commented on May 3, 2024

from dusk.

brandonferens avatar brandonferens commented on May 3, 2024

@MarceauKa that is what worked for me. I just used $app['config']->set('database.default','testing');. The thing I noticed is that the ChromeDriver was using the proper sqlite file, but the actual tests were defaulting to mysql. This forced the Dusk tests to use the proper db.

On another note, though, I use https://github.com/SRLabs/laravel-testing-utilities for updating the testing database. It really simplifies the testing setup.

from dusk.

mnabialek avatar mnabialek commented on May 3, 2024

@splatEric Yes, there was issue that Dusk was using the wrong file so it was not working in correct way. I've fixed it to reload environment after changing Dusk env into normal env so it's possible it's working fine now

from dusk.

ryancwalsh avatar ryancwalsh commented on May 3, 2024

@splatEric Thank you. #73 (comment) was super helpful. I looked at my database config array and saw that 'sqlite' was already set up that way, so all I needed to do was delete superfluous lines in my .env files.

from dusk.

rovshan-b avatar rovshan-b commented on May 3, 2024

Putting it here if someone has same problem and gets here using search engine.
Dusk accesses different database because you are starting server in another terminal for example using ./artisan serve. And when in Dusk you do $this->browse it sends request to your started server which in turn queries the database that it is configured to query (i.e. your local database). And later in your tests if you for example do $this->assertDatabaseHas Dusk this time uses the database it is configured to query (i.e. your test database). To solve this I use a shell script to run my tests. To use it first you need to put your Dusk config into file called .env.dusk. Remember to set listen port to something different than default, for example set it to 8001 (APP_URL=http://localhost:8001). And then create a file called (I do it on Linux) run_tests in the same directory as your Laravel app with following contents and make it runnable:

#!/bin/bash
nohup ./artisan serve --port=8001 --env=dusk > art_8001.hup &
./vendor/bin/phpunit
./artisan dusk --env=dusk
kill $(lsof -t -i:8001)
rm art_8001.hup

Now you can run it from terminal using ./run_tests. What it does is run artisan on port 8001 with an environment file .env.dusk so that it picks up configuration that you did for Dusk. After this it starts to run your phpunit and Dusk tests so that all queries go to same database. Hope this helps to someone who faces the same problem.

from dusk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.