php - 无法使用 App::Laravel 中的错误定义测试代码

标签 php exception testing laravel phpunit

我在测试 Laravel 应用程序时遇到问题,这些应用程序抛出由 global.php 中的 App::errors 声明处理的异常。当我在本地环境中使用 curl 对 Controller 执行请求时,一切都按预期工作,但是当在单元测试中对客户端执行相同的请求时,异常被返回而不是被 app::error 定义捕获。我在半大型 Laravel 应用程序中发现了这个问题,但我可以使用新生成的 Laravel 项目复制该行为。

在我唯一的 Controller 中,我抛出了这样的异常:

throw new Exception("My test exception");

app::error 声明如下所示:

App::error(function(Exception $exception, $code) {

    Log::error($exception);
    return Response::make("YAY from app:error!", 500);
});

我在运行时得到的错误是:

PHPUnit 4.2.6 by Sebastian Bergmann.

Configuration read from /Users/foo/testapp/testapp/phpunit.xml

E

Time: 64 ms, Memory: 9.75Mb

There was 1 error:

1) ExampleTest::testBasicExample
Exception: My test exception

/Users/foo/testapp/testapp/app/controllers/TestController.php:23
/Users/foo/testapp/testapp/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:231
/Users/foo/testapp/testapp/bootstrap/compiled.php:5799
/Users/foo/testapp/testapp/bootstrap/compiled.php:5787
/Users/foo/testapp/testapp/bootstrap/compiled.php:4986
/Users/foo/testapp/testapp/bootstrap/compiled.php:5345
/Users/foo/testapp/testapp/bootstrap/compiled.php:5011
/Users/foo/testapp/testapp/bootstrap/compiled.php:4999
/Users/foo/testapp/testapp/bootstrap/compiled.php:722
/Users/foo/testapp/testapp/bootstrap/compiled.php:703
/Users/foo/testapp/testapp/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/Users/foo/testapp/testapp/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:327
/Users/foo/testapp/testapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/Users/foo/testapp/testapp/app/tests/ExampleTest.php:12

The only bit of code I added to default TestCase.php is:

/**
 * Default preparation for each test
 */
public function setUp() {
    parent::setUp();
    Route::enableFilters();
}

唯一测试中的唯一代码:

$this->call('GET', 'test'); // test is the only route existing in this app

版本:

  • Laravel(框架):4.2.9
  • PHP单位:4.2.6
  • PHP:5.5.15 操作系统:
  • 操作系统 10.9.3

有人知道如何解决这个问题,以便在测试时实际触发 App:Error 吗?还是我在抛出和捕获异常的方式上做错了什么?

最佳答案

你问这个问题已经有一段时间了,但希望这能帮助那些正在寻找这个问题答案的人:

参见 Taylor Otwell 的 comment on this .转载如下:

“通过查看 PHPUnit 源代码,他们正在捕获异常并吞下它们,因此您的处理程序不会被捕获。

但是,我认为这可以通过解耦您的测试来解决。您确实想测试处理程序,并且无论如何都会完全单独抛出异常以隔离您的测试。例如:

App::error(function(ErrorType $e)
{
    App::make('ErrorTypeHandler')->handle($e);
});

现在您可以为 ErrorTypeHandler 类编写测试用例,与应用程序的其余部分分开。然后使用@expectedException 检查您的应用程序是否抛出了正确的异常。”

简单地说,如果您想测试它们,请单独实现您的异常处理程序。

关于php - 无法使用 App::Laravel 中的错误定义测试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037696/

相关文章:

php - 调用未定义函数 mysql_connect() 错误,即使安装了 PHP

java - 使用Spring MVC如何获取用户提交后的表单数据?

.net - 如何正确修改生成的XSD以克服导致异常 "cs0030:Unable to generate a temporary class"的已知.Net错误

android - 是否可以将 Android 应用程序设置为连接到开发服务器?

ruby-on-rails - rails 2.3.8 : How to functionally test the root route by requesting '/' ?

java - 模拟测试依赖注入(inject)的对象

php - 我如何使用 MySQL Join 按最后回复对论坛主题进行排序?

php - Foreach 通过多维数组嵌套

php - 从我在 WooCommerce 中的帐户编辑地址中删除账单电话和电子邮件字段

Haskell:非 IO monad 中的异常处理