我的测试用例函数是这样的:
public function testNoAjaxCall() { $this->call('POST','xyz',array()); $this->assertResponseStatus(403); }
在控制器操作中,这是路由到,我有这样的:
if(!Input::has('ajax') || Input::get('ajax') != 1) { // Drop all 'non-ajax' requests. App::abort(403,'Normal POST requests to this page are forbidden. Please explicitly tell me you\'re using AJAX by passing ajax = 1.'); }
1) RaceTest::testNoAjaxCall
Symfony\Component\HttpKernel\Exception\HttpException: Normal POST requests to this page are forbidden. Please explicitly tell me you’re using AJAX by passing ajax = 1.
[path\to\laravel]\vendor\laravel\framework\src\Illuminate\Foundation\Application.PHP:875
[redacted stack trace]
[path\to\laravel]\vendor\symfony\http-kernel\Symfony\Component\HttpKernel\Client.PHP:81
[path\to\laravel]\vendor\symfony\browser-kit\Symfony\Component\BrowserKit\Client.PHP:325
[path\to\laravel]\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.PHP:74
[path\to\laravel]\app\tests[testFileName].PHP:176
FAILURES!
Tests: 6,Assertions: 17,Errors: 1.
当然堆栈跟踪指向$this-> call(..);和App :: abort(..)
我的app / start / global.PHP文件中有一个HttpException错误处理程序,它在单元测试之外触发它时有效(例如直接向测试的URL发出POST请求),但单元测试似乎没有正确捕获异常甚至到达断言呼叫.
我错过了什么?
/** * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException * @expectedExceptionMessage Normal POST requests to this page are forbidden. Please explicitly tell me you\'re using AJAX by passing ajax = 1. */ public function testNoAjaxCall() { $this->call('POST',array()); }