Laravel 4 testing session in controllers

不羁岁月 提交于 2019-12-06 06:57:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21985636/laravel-4-testing-session-in-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!