I'm just starting out with looking at Dusk - and I'm testing some user functionality.
Below is my current test, however I'm trying to clean up after myself - for example the newly created user should be deleted from the database once it's done.
I've tried to use a tearDown method, but it doesn't seem to be be actually deleting it.
How would I typically go about spinning up temp models which need to be garbaged after?
<?php
namespace Tests\Browser;
use App\User;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class LoginTest extends DuskTestCase
{
protected $user = null;
public function testIfPublicUsersLogin()
{
$this->user = $user = factory(User::class)->create([
'is_student' => 0
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->assertVisible('#email')
->type('#email', $user->email)
->type('#password', 'secret')
->press('#loginButton')
->assertPathIs('/play');
});
}
public function tearDown()
{
if ($this->user) {
User::destroy($this->user->id);
//$this->user->delete();
}
}
}
There are multiple ways to achieve this:
use Illuminate\Foundation\Testing\DatabaseTransactions; in your php file and add use DatabaseTransactions; in your test classuse Illuminate\Foundation\Testing\DatabaseMigrations; in your php file and add use DatabaseMigrations; in your test classafterApplicationCreated and beforeApplicationDestroyed methods
instead to register callbacks<?php
namespace Tests\Browser;
use App\User;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class LoginTest extends DuskTestCase
{
protected $user = null;
public function testIfPublicUsersLogin()
{
$this->user = $user = factory(User::class)->create([
'is_student' => 0
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->assertVisible('#email')
->type('#email', $user->email)
->type('#password', 'secret')
->press('#loginButton')
->assertPathIs('/play');
$user->delete();
});
}
}
this code line $user->deletedelete your data after test. The tearDown method is useless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With