我有一个全新的laravel 5.4安装
我试图修改默认测试只是为了看到一个失败的测试.
测试/ ExampleTest.PHP
- class ExampleTest extends TestCase
- {
- /**
- * A basic test example.
- *
- * @return void
- */
- public function testBasicTest()
- {
- $response = $this->get('/ooops');
- $response->assertStatus(200);
- }
- }
我期待看到更详细的错误,如没有找到或定义的路线等,但只是这个错误说
- Time: 1.13 seconds,Memory: 8.00MB
- There was 1 failure:
- 1) Tests\Feature\ExampleTest::testBasicTest
- Expected status code 200 but received 404.
- Failed asserting that false is true.
- /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.PHP:51
- /var/www/tests/Feature/ExampleTest.PHP:21
没有有意义的错误很难做TDD(是的,我知道在这种情况下404就足够了,但大部分时间都不是这样).
有没有办法使堆栈跟踪与浏览器上显示的相同?或者至少接近那个,以便我知道我应该做的下一步是什么.
提前致谢.
对于Laravel 5.4,您可以使用Adam Wathan在
this gist中提供的disableExceptionHandling方法(源代码如下)
现在,如果你在测试中运行:
- $this->disableExceptionHandling();
您应该获得有助于您找到问题的完整信息.
对于Laravel 5.5及更高版本,您可以使用内置于Laravel中的withoutExceptionHandling方法
Adam Wathan的要点源代码
- <?PHP
- namespace Tests;
- use App\Exceptions\Handler;
- use Illuminate\Contracts\Debug\ExceptionHandler;
- use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
- abstract class TestCase extends BaseTestCase
- {
- use CreatesApplication;
- protected function setUp()
- {
- /**
- * This disables the exception handling to display the stacktrace on the console
- * the same way as it shown on the browser
- */
- parent::setUp();
- $this->disableExceptionHandling();
- }
- protected function disableExceptionHandling()
- {
- $this->app->instance(ExceptionHandler::class,new class extends Handler {
- public function __construct() {}
- public function report(\Exception $e)
- {
- // no-op
- }
- public function render($request,\Exception $e) {
- throw $e;
- }
- });
- }
- }