doushanlv5184 2017-07-04 13:01
浏览 62

Laravel Dusk,如何破坏测试之间的会话数据

I am getting started using Laravel Dusk for browser testing, and have created a couple of tests to test my login form. I have the following code:

class LoginTest extends DuskTestCase
{

public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/admin')
            ->type('email', 'inigo@mydomain.co.uk')
            ->type('password', 'MyPass')
            ->press('Login')
            ->assertSee('Loading...');
    });
}

public function testLoginFailure(){
    $this->browse(function (Browser $browser){

        $browser->visit('/admin/logout'); // I have to add this to logout first, otherwise it's already logged in for this test!

        $browser->visit('/admin')
            ->type('email', 'someemail@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}

See the comment. The first function runs fine, but when it comes to the second function, I have to logout first, since the user is already logged in as a result of running the first function. This came as a surprise to me as I thought unit tests were completely independent, with session data being destroyed automatically.

Is there a better way of doing this- some Dusk method that I'm missing perhaps- than having to call $browser->visit('/admin/logout'); ?

Thanks

EDIT Thanks for the 2 answers so far, which both seem valid solutions. I've updated the second function to the following:

public function testLoginFailure(){
    $this->createBrowsersFor(function(Browser $browser){
        $browser->visit('/admin')
            ->type('email', 'someshit@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}

Which does the job. So

  1. I can safely assume that this second browser only exists for the duration of this single function, correct?
  2. What are the obvious advantages/disadvantages of creating a second browser instance rather than using the teardown method?
  • 写回答

6条回答 默认 最新

  • dongshanyan0322 2017-07-04 13:14
    关注

    You can call

    $this->logout();
    

    InteractsWithAuthentication - Github

    Edit:

    This is the behaviour of the Dusk Test case, the primary browser instance remains for the other tests.

    A second solution, create a second browser instance, which will be destroyed after the single test

    See Dusk/TestCase #L103

    评论

报告相同问题?