Expectations exception fails in test but not in debug with PestPHP Laravel

76 views Asked by At

I can't do a test to verify that a series of errors in the work of a job throws a series of exceptions.

The Job calls an Action which makes a call to an API.

The test makes a fake of said call emulating a 500 error.

it('CallAnalyzerAction throws a communication error with slim', function () {
    $commandCenter = CommandCenter::factory()->create();
    $job_time = Carbon::now();
    $delayed_at = Carbon::now();
    $ulid = generateUlid();

    Http::fake(['*' => Http::response('Error', 500)]);

    AnalyzerCallJobV1::dispatch(
        $ulid,
        $commandCenter,
        $job_time,
        $delayed_at
    );

    $this->expectException(CallSlimException::class);
});

Action called

AnalyzerCallJobV1 handles call to an Action Class.

public function handle(): void
    {
        try {
            CallAnalyzerAction::run(
                $this->ulid,
                $this->commandCenter,
                $this->job_time,
                $this->initial_delayed_at,
                $this->debug,
                false,
                $this->tries,
                $this->attempts(),
                $this->key_multiple
            );
        } catch (Exception $e) {
            $this->handleException($e);
        }
    }

In this action there a piece of code is tested, if the response of a call external API gets an error on the HTTP client, serverErrror

if ($response->serverError()) {
   ray('Error 500');
   throw new CallSlimException("SLIM server error {$response->status()} $msg");
}

When I ran the test the answer was correct in the sense that the code hit the exception, since Ray shows me the message "Error 500"

However, the expectation fails.

Failed asserting that exception of type "App\Exceptions\CallSlimException" is thrown.
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:176

I'm lost with this problem

0

There are 0 answers