php – 在单元测试中处理Laravel HttpException

前端之家收集整理的这篇文章主要介绍了php – 在单元测试中处理Laravel HttpException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Laravel中进行单元测试的测试功能之一就是错误输出.我试图声明在没有满足某些条件的情况下请求特定页面会触发403 FORBIDDEN错误.

我的测试用例函数是这样的:

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.');

}

运行PHPunit返回以下内容

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请求),但单元测试似乎没有正确捕获异常甚至到达断言呼叫.

我错过了什么?

http://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedException应该已经解释过了.
/**
 * @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());

}

猜你在找的Laravel相关文章