kewwa's avatar
Level 3

Test Doesnt Display Errors Correctly

I'm trying to do test driven programming but can't get my errors to display correctly.

For example this code will pass $response = $this->get('/workername') Even though I don't have controller method created yet.

When I dd($response) then I there is exception error: #message: "Method App\Http\Controllers\WorkersController::show() does not exist"

Tried to google but never found any working solutions.

Kevin

0 likes
5 replies
eduphp8's avatar

I am confused, you said you don't have the method in your controller and the error says exactly that you don't have the method in your controller... it seems correct.

kewwa's avatar
Level 3

yes it is in $reaponse object and I can check it with dd() method but the problem is it doesn't pop up when I run my the test. Test will pass.

kyslik's avatar

Because in Exceptions/handler.php you are catching that exception so PHPUnit does not report any error.

You may "disable" error handling while testing using this snippet

public function render($request, Exception $exception)
{
    if (App::environment('testing')) {
        throw $exception;
    }
    ...
}

And better yet there is this video which may be even better.

1 like
kewwa's avatar
Level 3

Awesome you saved me, thanks alot !

Saneesh's avatar

From @kyslik's link the updated code for L6 with dusk:

in tests/DuskTestCase.php add disableExceptionHandling()

use Exception;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * To show the phpunit/dusk exceptions
     */
    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;
            }
        });
    }
    ...
}

In your testcase file tests/Browser/ViewAnotherUsersTweetsTest.php

class ViewAnotherUsersTweetsTest extends DuskTestCase
{
    use DatabaseMigrations;

    protected function setUp(): void {
        parent::setUp();
        $this->disableExceptionHandling();
    }
    ...
}

Please or to participate in this conversation.