我有一个全新安装的LARAVEL 5.4

I've tried to modify the default test just to see a failing test.

tests/ExampleTest.php

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/ooops');

        $response->assertStatus(200);
    }
}

I was expecting to see more detailed error like no route has been found or defined etc, but instead just this error saying

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

Its really hard to do TDD without meaningful error (yeah I know 404 in this case is enough, but most of the time its not the case).

是否有办法启用与浏览器上显示的堆栈跟踪相同的堆栈跟踪?或者至少离那个更近一点,这样我就知道下一步该怎么做了.

提前谢谢.

推荐答案

For Laravel 5.4 you can use disableExceptionHandling method presented by Adam Wathan in this gist (source code below)

Now if you run in your test:

$this->disableExceptionHandling();

你应该得到帮助你发现问题的完整信息.

For Laravel 5.5 and up you can use withoutExceptionHandling method that is built-in into Laravel

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;
            }
        });
    }
}

Laravel相关问答推荐

如何将错误从laravel恢复到inertiajs/vue 3应用程序?

如何定制有关密码被泄露的Laravel Breeze S消息?

Laravel 通过 API 嵌套 url 发布

如何通过 Controller 访问 Laravel 中 Model 的值?

使用命令行界面停止 laravel 服务器

Laravel 保存一对多关系

上传时Laravel正在旋转图像

在 Laravel 4 迁移中创建 MYSQL 过程

Laravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败

Laravel Eloquent:计算总价的最佳方法

如何使用中间件将标头添加到响应中?

Laravel 中的填充方法不起作用?

Laravel 5 干预中的图像验证

Laravel 全部返回 ID 变为 0

Laravel 4 上传图片表单

扩展模型 == 扩展 Eloquent?

Laravel 更新语法 - 使用数组更新记录

在 Laravel 5.4 中向多个抄送收件人发送Electron邮件

使用 Queue::fake() 测试监听器

使用 Carbon 将小时转换为 PM 和 AM