我重新安装了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);
}
}
我期待看到更详细的错误,例如
no route has been found or defined
等,但只是此错误说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在这种情况下就足够了,但是在大多数情况下不是这样)。
有没有一种方法可以使stacktrace与浏览器上显示的相同?或者至少更接近那个,以便我知道下一步应该做什么。
提前致谢。
最佳答案
对于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;
}
});
}
}
关于laravel - Laravel:如何在PhpUnit上启用stacktrace错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945622/