Jai*_*cap 14 tdd phpunit laravel laravel-5 laravel-5.4
我有一个全新的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我期待看到更详细的错误,如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
Run Code Online (Sandbox Code Playgroud)
没有有意义的错误很难做TDD(是的,我知道在这种情况下404就足够了,但大部分时间都不是这样).
有没有办法使堆栈跟踪与浏览器上显示的相同?或者至少接近那个,以便我知道我应该做的下一步是什么.
提前致谢.
Mar*_*łek 24
对于Laravel 5.4,您可以使用disableExceptionHandlingAdam Wathan在此要点中提供的方法(源代码如下)
现在,如果你在测试中运行:
$this->disableExceptionHandling();
Run Code Online (Sandbox Code Playgroud)
您应该获得有助于您找到问题的完整信息.
对于Laravel 5.5及更高版本,您可以使用withoutExceptionHandling内置于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;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
如果您碰巧使用了Laravel 5.5及更高版本,则可以使用内置方法:
$this->withoutExceptionHandling();
$this->withExceptionHandling();
Run Code Online (Sandbox Code Playgroud)
在您的setUp方法中,或者在您的测试方法中。它们具有以下特征。
为了进行快速而肮脏的调试,您还可以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
]);
...
}
Run Code Online (Sandbox Code Playgroud)
有一个介绍这些细节的简单课程。
| 归档时间: |
|
| 查看次数: |
6786 次 |
| 最近记录: |