Comments (14)
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.
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.
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.
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.
Same here!
from dusk.
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.
@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.
Thanks, I'll check it out.
from dusk.
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.
from dusk.
@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.
@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.
@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.
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)
- A way to have different environment variables HOT 2
- Dusk v7.8.0 breaks Pest support
- Downloading ChromeDriver 115+ fails HOT 7
- Dusk 7.9.0 \ chromedriver.exe path not found HOT 11
- [7.9.1] unknown error: cannot find Chrome binary HOT 5
- `--stop-on-failure` option no longer seems to work HOT 1
- Driver won't download with Telescope enabled HOT 8
- unknown error: cannot find Chrome binary HOT 1
- Error This version of ChromeDriver only supports Chrome version 114 Current browser version is 118.0.5975.0 HOT 1
- Assertion failure can lead to timeout if using multiple browsers HOT 5
- Test class with no tests gives non-zero stderr status, but no warning to stdout and shows all tests pass HOT 5
- Unexpected behaviour when working with multiple checkboxes HOT 1
- waitForTextIn() broken in dusk 7.11.2 HOT 2
- Unknown option "--ansi" and "--no-ansi"
- Laravel Dusk doesn't trigger on @input when attached to <select> only on @change when using Vue HOT 2
- When running `php artisan dusk` the tests do not complete. They just hang. - MacOS Sonoma 14.2.1, Chip: M3 Pro HOT 9
- Throwing an exception in a dusk test inside the browser callback results in timeouts HOT 2
- vueAttribute does not work with Vue 3 and vite build. only with vite dev. HOT 6
- Failed to load assets in Sail HOT 4
- Dusk slows down after call to dump() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dusk.