I'm using Dusk to do a simple login test.
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.
Archive .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk
DB_USERNAME=root
DB_PASSWORD=123456
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
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?
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.
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'));
@alaric
I changed the .env.dusk.testing
file to .env.dusk.local
I ran the php artisan serve
and created a new user in the laravel_dusk database.
I ran the php artisan serve
again and then the php artisan dusk
to run the tests and create a new user with the same email but in the database laravel_dusk_test and it continues registering in laravel_dusk.
Instead of using a mysql database I would recommend using a temporary sqlite db since it gets created and destroyed during tests.
you will have to have a sqilte config in your database.php that points at an actual .sqlite file that you have in your installation
so copy the sqlite
config in database.php
and then paste it, name it sqlite_dusk
maybe, then for the location of the db put it as storage_path('dusk.sqlite')
or something like that. Then create a blank dusk.sqlite
file in the root of your storage
folder.
Then in your .env.dusk
set:
DB_CONNECTION=sqlite_dusk
Hope that helps!
I had similar issue when i was using \App\User::truncate() in my dusk file.
It truncates the dev database instead of the test database. I don't know exactly why because php artisan dusk replace the .env file with the .env.dusk.local for the test. Adding the whole configuration of the DB in the .env.dusk.local did the job for me.
So first, if your file .env have APP_ENV=local
then rename your dusk file to .env.dusk.local
Second, in your .env.dusk.local be sure to use the whole configuration of your database:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tdd_test
DB_USERNAME=your_username
DB_PASSWORD=your_password
instead of
DB_CONNECTION=test // configuration of test in the /config/database.php
then run your test with
php arisan dusk