phplaravellaravel-duskteardown

laravel dusk tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown()


public function tearDown()
    {
        $this->browse(function (Browser $browser) {
            $browser->click('#navbarDropdown')
                    ->click('.dropdown-item');
        });


        parent::tearDown();
    }

When I apply the tearDown() method to my test class I get an error telling me the tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown() What am I doing wrong?

Every time I run a test I need to login. I want to login in the setUp() method and the log out again in the tearDown, so I can execute my tests independently.

This is my setUp() method

use databaseMigrations;
    public function setUp(): void
    {
        parent::setUp();

        $this->seed('DatabaseSeeder');

        $this->browse(function (Browser $browser) {
            $browser->visit('/admin')
                    ->type('email', 'admin@admin.com')
                    ->type('password', 'admin')
                    ->press('Login');
        });
    }

The setUp() method works just fine. Even when I don't add any code to the tearDown() method, except the parent::tearDown();, I get an error, so what am I doing wrong in my tearDown() method?

public function tearDown()
    {

        parent::tearDown();
    }

Solution

  • You are missing the : void on tearDown():

    public function tearDown(): void
    {
      parent::tearDown();
    }
    

    You have setUp() correct, but both methods, as method of the parent class, need to be compatible and omitting the : void isn't.

    Any time you see that error, it's best to check the source of the class you're extending. By inheritance, that is

    Illuminate\Foundation\Testing\TestCase.php

    /**
     * Setup the test environment.
     *
     * @return void
     */
    protected function setUp(): void
    {
      ...
    }
    ...
    /**
     * Clean up the testing environment before the next test.
     *
     * @return void
     */
    protected function tearDown(): void
    {
      ...
    }