加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:56:28 所属栏目:大数据 来源:网络整理
导读:我有一个全新的laravel 5.4安装 我试图修改默认测试只是为了看到一个失败的测试. 测试/ ExampleTest.php class ExampleTest extends TestCase{ /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this-get('
我有一个全新的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) TestsFeatureExampleTest::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 AppExceptionsHandler;
use IlluminateContractsDebugExceptionHandler;
use IlluminateFoundationTestingTestCase 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;
            }
        });
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读