'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();
        }
    }
}


Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paras
Solution 2 Kerim Ku?cu