Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 testing session in controllers

I have a problem with testing controllers in Laravel 4.

I have the next code:

public function getRemind()
{
    $status = \Session::get('status');
    $error = \Session::get('error');
    $email = \Session::get('email');

    return \View::make('admin/reminds/remind_form', compact('status', 'error', 'email'));
}

And I want to test if correct data passed in views by controller:

public function testGetRemind()
{
    \Session::set('status', 'status');
    \Session::set('error', 'error');
    \Session::set('email', 'email');
    $response = $this->action('GET', 'Admin\RemindersController@getRemind');
    $this->assertTrue($response->isOk(), 'Get remind action is not ok');
    $this->assertViewHas('status', 'status');
    $this->assertViewHas('error', 'error');
    $this->assertViewHas('email', 'email');
}

But this doesn't work.

Also I can't mock Session-class, because it not allowed by framework - there are a lot of errors when I try doing it.

like image 760
violarium Avatar asked Dec 25 '22 13:12

violarium


1 Answers

Call Session::start() at the start of your test. When you call an URL in a test, the session is started if it has not already been started, wiping any existing data put into it.

Since v4.1.23, you can also do $this->session($arrayOfSessionData), which handles the starting of the session for you.

like image 142
Andreas Avatar answered Jan 05 '23 10:01

Andreas