New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Why Dusk Page Test uses DatabaseMigrations instead of DatabaseTransactions? #110
Comments
Transactions started in your test files won't work because Dusk operates under a separate process. Would be nice though if this weren't the case |
Yes, it is not possible to use transactions with Dusk for that reason, a possible solution could be to trigger a command to truncate all the tables after the test finishes, it could be faster than re-running the migrations each time. |
This works for me: <?php
namespace Tests\Browser;
use App\User;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends DuskTestCase
{
use DatabaseTransactions;
/**
* A basic browser test example.
*
* @return void
*/
public function testBasicExample()
{
$user = factory(User::class)->create([
'email' => 'demo@example.com',
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->type('login', $user->email)
->type('password', '123456')
->press('Login')
->assertPathIs('/');
});
}
} |
@vinayakkulkarni I'm not quite sure how that could be working. The creation of the user record and the use of it in the browser are happened in two different processes. This means the created user is part of a database transaction which is not committed and thus not accessible by the browser process. |
well, i have 2 .env files
I got 2 databases,
both DB's are properly seeded |
@vinayakkulkarni Is the |
@DivineOmega : yes. |
@vinayakkulkarni if your test db was pre seeded before running tests, then yes your tests would pass. That has nothing to do with migrations though and your user create code above still would have had no effect inside your browser tests |
@georaldc Precisely what I was going to say. Thanks. |
You can lose the model factory and the use DatabaseTransaction, then. They're useless. |
Here's how I do it: <?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
public function setUp()
{
Parent::setUp();
$this->user = factory(\App\User::class)->make(['password' => bcrypt('pass123')]) ;
$this->user->save();
}
/** @test */
public function when_users_login_successfully_they_are_redirected_to_root()
{
$this->browse(function (Browser $browser) {
$browser->visit('/login')
->type('email', $this->user->email)
->type('password', 'pass123')
->press('Login')
->assertPathIs('/');
});
// after test is done. Delete test user
$this->user->delete();
} |
@aboustayyef - why not delete the user in a |
That's how i do it.
|
But in this way, it says undefined variable for $user.
Data generated by this path is deleted after use. |
try this you were not passing $user with |
you don't need to use it |
Similar to Feature/Unit test, I don't want the database to be populated with test data.
It seem it's more appropriate using DatabaseTransactions instead of DatabaseMigration in running dusk test.
See: https://laravel.com/docs/5.4/dusk#creating-browsers
Can someone enlighten me, why? Thanks in advance.
cc @sileence
The text was updated successfully, but these errors were encountered: