在单元测试中处理Laravel HttpException [英] Handling Laravel HttpException in Unit Testing
问题描述
我在Laravel中进行单元测试的测试功能之一经常出错.我试图断言在不满足某些条件的情况下请求特定页面会触发403 FORBIDDEN错误.
One of my test functions for unit testing in Laravel keeps erroring out. I'm trying to assert that requesting a specific page without certain conditions being met triggers a 403 FORBIDDEN error.
我的测试用例功能是这样的:
My test case function is this:
public function testNoAjaxCall() {
$this->call('POST', 'xyz', array());
$this->assertResponseStatus(403);
}
在发送到的控制器动作中,我有这个:
In the controller action this is routed to, I have this:
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.');
}
正在运行的phpunit返回以下内容:
Running phpunit returns with the following:
1)RaceTest :: testNoAjaxCall
1) RaceTest::testNoAjaxCall
Symfony \ Component \ HttpKernel \ Exception \ HttpException:禁止对此页面进行常规POST请求.请通过传递ajax = 1明确告诉我您正在使用AJAX.
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
[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\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\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]\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:74
[path \ to \ laravel] \ app \ tests [testFileName] .php:176
[path\to\laravel]\app\tests[testFileName].php:176
失败!
测试:6,断言:17,错误:1.
Tests: 6, Assertions: 17, Errors: 1.
当然,堆栈跟踪指向了$this->call(..);
和App::abort(..)
Of course the stack trace points back to $this->call(..);
and App::abort(..)
我的app/start/global.php
文件中有一个HttpException错误处理程序,可以在单元测试之外触发它时起作用(例如,直接向已测试的URL发出POST
请求),但是单元测试似乎无法正确捕获异常,甚至到达断言调用.
I have a HttpException error handler in my app/start/global.php
file, which works when triggering it outside of unit testing (e.g. making a POST
request directly to the tested URL), but unit testing doesn't seem to correctly catch the exception or even reach the assert call.
我想念什么?
推荐答案
http ://phpunit.de/manual/3.7/zh-CN/appendixes.annotations.html#appendixes.annotations.expectedException 应该已经对此进行了解释.
http://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedException should already explain it.
/**
* @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', 'xyz', array());
}
这篇关于在单元测试中处理Laravel HttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!