Laravel 4控制器中的测试会话 [英] Laravel 4 testing session in controllers

查看:47
本文介绍了Laravel 4控制器中的测试会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 4中测试控制器有问题.

I have a problem with testing controllers in Laravel 4.

我有下一个代码:

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');
}

但这是行不通的.

不能模拟会话类,因为框架不允许这样做-尝试时会出现很多错误.

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

推荐答案

在测试开始时致电Session::start().当您在测试中调用URL时,如果会话尚未启动,则将启动该会话,并清除其中已存入的所有数据.

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.

从v4.1.23开始,您还可以执行$this->session($arrayOfSessionData),它为您处理会话的开始.

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

这篇关于Laravel 4控制器中的测试会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆