jeudi 6 octobre 2016

Why do I have to reload Auth::user() when doing integration testing

This is a follow up of How to wait for a page reload in Laravel integration testing

What I am doing is to edit a user's profile and then redisplay the view.

My profile action: (UserController)

public function profile(){
    return view('user/profile');
}

The view contains code like



now during my test, the old (unchanged) user data is displayed.

The test:

protected function editUserProfile()
{
    $this->visit('/user/profile');
    $firstName = $this->faker->firstname;
    $lastname = $this->faker->lastname;

    $this->within('#userEditForm', function() use ($firstName, $lastname) {
        $this->type($firstName, 'firstname');
        $this->type($lastname, 'surname');
        $this->markTestSkipped();
        $this->press('Save')
            ->seePageIs('/user/profile')
            ->see($firstName)   # here the test fails
            ->see($lastname);
    });
}

When I change the UserController like this:

public function profile(){
    Auth::setUser(Auth::user()->fresh());
    return view('user/profile');
}

everything works fine.

Now I want to understand, why that is like this.

Why does the integration test behave differently to the browser in that case? Is there a better way to align that behavior so the tests do only fail if there is a "real problem"? Or is my code just bad?

Aucun commentaire:

Enregistrer un commentaire