Framework: Can't set session value in tests

Created on 25 Feb 2014  路  6Comments  路  Source: laravel/framework

I think that this issue come with one of the last versions of 4.1.*. I am not able to set a session value in a test. I'm pretty sure that I was able to do that before.

// HomeControllerTest.php
public function testSetSessionValueInHomePage()
{
    Session::set('role', 'admin');
    $this->call('GET', 'home');
}

// HomeController.php
public function getHome()
{
    Session::get('role'); // return null
}

I could use mocks but that's not my goal in my integration tests and that's not simple to mock. I have to do that for now :

$sessionStore = m::mock('Illuminate\Session\Store');
Session::shouldReceive('isStarted')->andReturn(true);
Session::shouldReceive('driver')->andReturn($sessionStore);
Session::shouldReceive('get')->with('role')->andReturn('admin');

Most helpful comment

Added session helper to TestCase.

$this->session(['foo' => 'bar']);

Also:

$this->flushSession();

All 6 comments

Are you using the array driver?

For testing, yes.

Error?

When I set a session value in a controller test, I can't get that value in the controller. Like if it was never set.

Added session helper to TestCase.

$this->session(['foo' => 'bar']);

Also:

$this->flushSession();

Great ! Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

progmars picture progmars  路  3Comments

ghost picture ghost  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments