【发布时间】:2014-11-14 13:19:33
【问题描述】:
好的,我在为“不正确的凭据”登录(即缺少密码)编写功能测试时遇到了麻烦。
背景是我的路线配置:
Route::get('login', array('as' => 'login', 'uses' => 'SessionController@create'));
Route::resource('session', 'SessionController');
这是我的 SessionController::store() 方法中的内容:
public function store()
{
$credentials = Input::only('email', 'password');
try {
$this->loginForm->validate($credentials);
if (!Auth::attempt(array('email' => $credentials['email'], 'password' => $credentials['password']))) {
return Redirect::back()->exceptInput('password')->with('flash_message', 'Invalid credentials');
}
return Redirect::to('/')->with('flash_message', 'Welcome back');
}
catch (FormValidationException $e)
{
return Redirect::back()->exceptInput('password')->withErrors($e->getErrors());
}
}
当我将带有 POST 操作的表单提交到 /session 时,上述 store() 控制器在浏览器中完美运行(!)。引发表单验证错误,我被重定向回 /login 并在我的视图中使用 $errors 变量正确填充了相关消息(“密码字段是必需的”):
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
但是,当尝试在功能测试中复制上述预期的工作行为时,我遇到了问题。这是我的测试的基本大纲:
public function testIncorrectCredentialsLogin()
{
$this->client->request('GET', '/login');
$this->assertTrue($this->client->getResponse()->isOk());
$credentials = array(
'email' => 'me@example.com',
'password' => ''
);
$this->client->request('POST', '/session', $credentials);
$this->assertRedirectedTo('login', array('errors'));
$crawler = $this->client->followRedirect();
$this->assertSessionHasErrors();
$this->assertHasOldInput();
$this->assertViewHas('errors'); // Fails
}
在我的功能测试中,我尝试复制浏览器中成功发生的情况。我首先 GET /login,然后在缺少凭据的情况下对 SessionController::store() 进行 /POST,这会导致验证错误并重定向回 /login。到目前为止一切正常,但是当我按照此重定向返回时,我会查看呈现的登录页面内容/HTML,并且我没有设置 $errors 变量,因此会话中没有可用的消息/页面中显示。
谁能告诉我可能会发生什么?
提前致谢。
【问题讨论】:
标签: php laravel laravel-4 integration-testing functional-testing