lundi 9 octobre 2017

Storing and retrieving session variables in unit/feature tests in Laravel

In a Laravel feature test, I'm trying to store a variable in a session so that I can use it in the rest of my tests, like so:

public function testLogin()
{
    $response = $this->json('POST', '/login', $myCredentials);

    $response
        ->assertStatus(200)
        ->assertJson([
            'token' => true
        ]);

    session(['token' => $response['token']]);
}

When I run "phpunit" in the command line, I get this error:

PHP Fatal error:  Uncaught ReflectionException: Class session does not exist in /vendor/laravel/framework/src/Illuminate/Container/Container.php:752

Apparently the "session()" global helper doesn't work in test classes. I also tried to work with the class directly by using "Illuminate\Session" or just "\Session", but both returned in "not found" errors. How can I store and retrieve session variables within test classes?

Aucun commentaire:

Enregistrer un commentaire