jeudi 12 juillet 2018

Dusk user auth session is shared across test methods in a Test Class in Laravel 5.6

So I have multiple methods in a single Test Class.

In the first method the factory user is created goes to login fills login credentials and assertPathis('/home'). This method runs without any problem.

public function test_user_should_see_home()
    {

        $user = factory(User::class)->create([
            'name' => 'first',
            'email' => 'abc@gmail.com',

        ]);


        $this->browse(function ($browser) use($user){
            $browser->visit('/login')
                    ->type('email', $user->email)
                    ->type('password', 'secret')
                    ->press('Login')
                    ->assertPathIs('/home');
        });

    }

In the second method again factory user is created goes to login and fills credentials and makes some assertion.

public function test_user_should_see_login()
    {

        $user = factory(User::class)->create([
            'name' => 'second',
            'email' => 'abcd@gmail.com',
        ]);

        $this->browse(function ($browser) use($user){
            $browser->visit('/login')
                    ->assertPathIs('/login');
        });
    }

But the second method fails with the error:

Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}

And I looked at the screenshot error it shows the user is at home page and since the logged in user cannot go to login, the test fails.

So how can I make dusk to treat every method as a separate test rather then sharing session across methods ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire