Home > Net >  Unit/Feature test in Laravel returns status code 200 but browser returns status code 500
Unit/Feature test in Laravel returns status code 200 but browser returns status code 500

Time:10-12

Why Feature test in "test_indicador_descuadre_eerr" returns me status code 200 but not 500?

If I test this directly in the browser I get status code 500.

I forced the error giving a dd() into the service code.

How this is posible? Here is the code for test passes regardless of browser getting status code 500.

<?php

namespace Feature;

use App\User;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class DashboadTest extends TestCase
{

    public function test_indicador_descuadre_eerr()
    {
        $user = factory(User::class)->create();
        $holding_id = 0;
        $cliente_id = 0;
        $periodo = 2022;
 
        $response = $this->actingAs($user)
        ->getJson('/dt-ajax/descuadre-eerr?holding_id=' . $holding_id . '&cliente_id=' . $cliente_id . '&periodo=' . $periodo)
        ->assertStatus(200);
    }

}

Browser console log screenshot

CodePudding user response:

dd() is a Symfony function to dump data to the screen. If you look at the code for the function (vendor/symfony/var-dumper/Resources/functions/dump.php):

if (!function_exists('dd')) {
    /**
     * @return never
     */
    function dd(...$vars): void
    {
        if (!in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && !headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error');
        }

        foreach ($vars as $v) {
            VarDumper::dump($v);
        }

        exit(1);
    }
}

You'll see that if PHP_SAPI does not contain cli or phpdbg, as test packages do, it will return a 500 Internal Server Error status.

  • Related