Laravel:如何在 PhpUnit 上启用堆栈跟踪错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41945622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to enable stacktrace error on PhpUnit
提问by Jaime Sangcap
I have a fresh installation of laravel 5.4
我全新安装了 laravel 5.4
I've tried to modify the default test just to see a failing test.
我试图修改默认测试只是为了查看失败的测试。
tests/ExampleTest.php
测试/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
我期待看到更详细的错误,如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
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).
在没有有意义的错误的情况下执行 TDD 真的很难(是的,我知道在这种情况下 404 就足够了,但大多数时候情况并非如此)。
Is there a way to enable the stacktrace the same as the one displayed on the browser? Or at least closer to that one so that I know what's the next step I should do.
有没有办法启用与浏览器上显示的堆栈跟踪相同的堆栈跟踪?或者至少更接近那个,以便我知道下一步应该做什么。
Thanks in advance.
提前致谢。
回答by Marcin Nabia?ek
For Laravel 5.4 you can use disableExceptionHandling
method presented by Adam Wathan in this gist(source code below)
对于 Laravel 5.4,您可以使用disableExceptionHandling
Adam Wathan 在此要点中提供的方法(下面的源代码)
Now if you run in your test:
现在,如果您在测试中运行:
$this->disableExceptionHandling();
you should get full info that will help you to find the problem.
您应该获得可以帮助您找到问题的完整信息。
For Laravel 5.5 and up you can use withoutExceptionHandling
method that is built-in into Laravel
对于 Laravel 5.5 及更高版本,您可以使用withoutExceptionHandling
Laravel 内置的方法
Source code of Adam Wathan's gist
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;
}
});
}
}
回答by Patrick.SE
If you happen to use Laravel 5.5 and up, you can use the built-in methods:
如果您碰巧使用 Laravel 5.5 及更高版本,则可以使用内置方法:
$this->withoutExceptionHandling();
$this->withExceptionHandling();
Either in your setUp method, or in your test method's. They are defined in the following trait.
无论是在您的 setUp 方法中,还是在您的测试方法中。它们在以下trait中定义。
For quick and dirty debugging, you can also use the dump
method on the response
object:
为了快速和脏调试,您还可以dump
在response
对象上使用该方法:
/** @test */
public function it_can_delete_an_attribute()
{
$response = $this->json('DELETE', "/api/attributes/3");
$response->dump()->assertStatus(200);
$this->assertDatabaseMissing('table', [
'id' => $id
]);
...
}
There is a laracastlesson that covers these details.
有一个laracast课程涵盖了这些细节。