在单元测试中处理Laravel HttpException [英] Handling Laravel HttpException in Unit Testing

查看:88
本文介绍了在单元测试中处理Laravel HttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆