[英]Laravel Controller UNIT Testing Issue?
我正在创建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');
}
}
现在,他测试为上述两种方法创建的ive是...
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']);
}
初始测试成功通过,但第二次测试失败。 经过一番调查后,它表明Validation没有通过,这意味着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']);
}
通过检查会话来调试测试,就像我怀疑的那样,输入没有被填充,这可能是什么原因? 验证消息已返回。
$response = $this->route('POST', 'guest.report_issue', array(
'full_name' => 'Foo',
'email' => 'Man@Chu.com',
'issue' => 'FooBar'));
您可以将参数作为数组传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.