Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to enable stacktrace error on PhpUnit

Tags:

I have a fresh installation of 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).

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.

like image 806
Jaime Sangcap Avatar asked Jan 30 '17 21:01

Jaime Sangcap


People also ask

What is testcase in Laravel?

TestCase.php: The TestCase.php file is a bootstrap file for setting up the Laravel environment within our tests. This allows us to use Laravel facades in tests and provides the framework for the testing helpers, which we will look at shortly.

How do I create a test application in Laravel?

If you have the Laravel installer set up, you can create a new test application by running: Alternatively, you can create a new application by using Composer directly: Other installation options can also be found in the Laravel documentation. The first step when using PHPUnit is to create a new test class.

How do I archive a Validation rule in PHPUnit?

One way we can archive this by creating test cases for each validation rule. This approach is fine, but when we have many fields and validation rules, it becomes hard to write and maintain all test cases for each validation rule. Instead, we can use the power of PHPUnit’s data providers.

How to test a PHP application using PHPUnit?

For testing, you will be using PHPUnit. All your tests will be located in the tests directory of the application. The folder structure we will take will be one that somewhat matches the application structure. The code to test your controllers will be in the tests / Controller directory.


2 Answers

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

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

Source code of Adam Wathan's gist

<?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;             }         });     } } 
like image 61
Marcin Nabiałek Avatar answered Sep 21 '22 20:09

Marcin Nabiałek


If you happen to use Laravel 5.5 and up, you can use the built-in methods:

$this->withoutExceptionHandling(); $this->withExceptionHandling(); 

Either in your setUp method, or in your test method's. They are defined in the following trait.

For quick and dirty debugging, you can also use the dump method on the response object:

/** @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 laracast lesson that covers these details.

like image 45
Patrick.SE Avatar answered Sep 19 '22 20:09

Patrick.SE