我正在创建一个 Laravel 4 应用程序,但遇到了一些障碍。在为我的控制器编写测试时,我注意到出于某种奇怪的原因,它似乎永远无法验证。这是我的(精简的)控制器代码。
<?php use Controllers\Base\PublicController;
class GuestController extends PublicController {
/**
* Display the report issue form.
*
* @return null
*/
public function getReportIssue()
{
$this->layout->title = Lang::get('app.report_issue');
$this->layout->content = View::make('guest.report_issue');
}
public function postReportIssue()
{
$rules = [
'full_name' => 'required|min:2|max:100',
'email' => 'required|email',
'issue' => 'required|min:10|max:1000',
];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::route('guest.report_issue')
->withInput()
->withErrors($validator->messages());
}
return Redirect::route('guest.reported_issue')
->with('msg', 'Okay');
}
}
现在他为上述两种方法创建的测试是......
public function testHandleFailReportIssue()
{
Input::replace([
'full_name' => '',
'email' => '',
'issue' => '',
]);
$this->call('POST', 'report-issue');
$this->assertRedirectedToRoute('guest.report_issue');
$this->assertSessionHasErrors(['full_name', 'email', 'issue']);
}
public function testHandlePassReportIssue()
{
Input::replace([
'full_name' => 'John Doe',
'email' => 'john@localhost.dev',
'issue' => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar'
]);
$this->call('POST', 'report-issue');
$this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}
第一个测试成功通过,但第二个测试失败。经过一番调查,它表明验证没有通过,这意味着该Input::replace()
方法没有完成它的工作,因为我注入了有效的请求值。也许我错过了什么?
[编辑]
我决定这样做
public function testHandlePassReportIssue()
{
Input::replace([
'full_name' => 'John Doe',
'email' => 'john@flashdp.com',
'issue' => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar',
]);
$response = $this->route('POST', 'guest.report_issue');
dd($this->app['session']->get('errors'));
$this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}
在通过检查会话进行调试的测试中,就像我怀疑的那样,输入没有被填充,这可能是什么原因?验证消息已返回。