Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法使用Laravel中的App::错误定义测试代码_Php_Exception_Testing_Laravel_Phpunit - Fatal编程技术网

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

Php 无法使用Laravel中的App::错误定义测试代码,php,exception,testing,laravel,phpunit,Php,Exception,Testing,Laravel,Phpunit,我在测试Laravel应用程序时遇到了一些问题,这些应用程序抛出的异常由global.php中的App::errors声明处理。当我在本地环境中使用curl向控制器执行请求时,一切都按预期工作,但在单元测试中对客户端执行相同的请求时,将返回异常,而不是被app::error定义捕获。我在一个半大型的laravel应用程序中发现了这个问题,但我可以用新生成的laravel项目复制这个行为 在我唯一的控制器中,我抛出如下异常: throw new Exception("My test excepti

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

在我唯一的控制器中,我抛出如下异常:

throw new Exception("My test exception");
App::error(function(Exception $exception, $code) {

    Log::error($exception);
    return Response::make("YAY from app:error!", 500);
});
错误声明如下所示:

throw new Exception("My test exception");
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 唯一测试中的唯一代码:

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

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

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

你问这个问题已经有一段时间了,希望这能帮助人们找到答案:

看泰勒·奥特威尔的。转载如下:

“从PHPUnit源来看,它们正在捕获异常并将其吞并,这样您的处理程序就不会被捕获

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

App::error(function(ErrorType $e)
{
    App::make('ErrorTypeHandler')->handle($e);
});
现在,您可以为ErrorTypeHandler类编写独立于应用程序其余部分的测试用例。然后使用@expectedException检查应用程序是否引发了正确的异常。”

简单地说,如果要测试异常处理程序,请分别实现它们