Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting models from database after Laravel Dusk tests have run?

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();
        }
    }
}
like image 765
owenmelbz Avatar asked Oct 21 '25 16:10

owenmelbz


2 Answers

There are multiple ways to achieve this:

  1. Use the DatabaseTransactions trait so that there's a transaction rollback after every test. To do so add: use Illuminate\Foundation\Testing\DatabaseTransactions; in your php file and add use DatabaseTransactions; in your test class
  2. You might want to use the DatabaseMigrations trait if you want to migrate and migrate rollback before and after every test rather than wrap them into transactions. To do so add: use Illuminate\Foundation\Testing\DatabaseMigrations; in your php file and add use DatabaseMigrations; in your test class
  3. If you want to use custom setup and teardown methods, use the afterApplicationCreated and beforeApplicationDestroyed methods instead to register callbacks
like image 77
Paras Avatar answered Oct 23 '25 05:10

Paras


<?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.

like image 29
Kerim Kuşcu Avatar answered Oct 23 '25 07:10

Kerim Kuşcu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!