Laravel:如何在PhpUnit上启用stacktrace错误

前端之家收集整理的这篇文章主要介绍了Laravel:如何在PhpUnit上启用stacktrace错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个全新的laravel 5.4安装

我试图修改默认测试只是为了看到一个失败的测试.

测试/ ExampleTest.PHP

  1. class ExampleTest extends TestCase
  2. {
  3. /**
  4. * A basic test example.
  5. *
  6. * @return void
  7. */
  8. public function testBasicTest()
  9. {
  10. $response = $this->get('/ooops');
  11.  
  12. $response->assertStatus(200);
  13. }
  14. }

我期待看到更详细的错误,如没有找到或定义的路线等,但只是这个错误

  1. Time: 1.13 seconds,Memory: 8.00MB
  2.  
  3. There was 1 failure:
  4.  
  5. 1) Tests\Feature\ExampleTest::testBasicTest
  6. Expected status code 200 but received 404.
  7. Failed asserting that false is true.
  8.  
  9. /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.PHP:51
  10. /var/www/tests/Feature/ExampleTest.PHP:21

没有有意义的错误很难做TDD(是的,我知道在这种情况下404就足够了,但大部分时间都不是这样).

有没有办法使堆栈跟踪与浏览器上显示的相同?或者至少接近那个,以便我知道我应该做的下一步是什么.

提前致谢.

对于Laravel 5.4,您可以使用Adam Wathan在 this gist中提供的disableExceptionHandling方法(源代码如下)

现在,如果你在测试中运行:

  1. $this->disableExceptionHandling();

您应该获得有助于您找到问题的完整信息.

对于Laravel 5.5及更高版本,您可以使用内置于Laravel中的withoutExceptionHandling方法

Adam Wathan的要点源代码

  1. <?PHP
  2.  
  3. namespace Tests;
  4.  
  5. use App\Exceptions\Handler;
  6. use Illuminate\Contracts\Debug\ExceptionHandler;
  7. use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
  8.  
  9. abstract class TestCase extends BaseTestCase
  10. {
  11. use CreatesApplication;
  12.  
  13. protected function setUp()
  14. {
  15. /**
  16. * This disables the exception handling to display the stacktrace on the console
  17. * the same way as it shown on the browser
  18. */
  19. parent::setUp();
  20. $this->disableExceptionHandling();
  21. }
  22.  
  23. protected function disableExceptionHandling()
  24. {
  25. $this->app->instance(ExceptionHandler::class,new class extends Handler {
  26. public function __construct() {}
  27.  
  28. public function report(\Exception $e)
  29. {
  30. // no-op
  31. }
  32.  
  33. public function render($request,\Exception $e) {
  34. throw $e;
  35. }
  36. });
  37. }
  38. }

猜你在找的Laravel相关文章