abkrim's avatar
Level 13

Expectations exceptions how to with Pest?

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

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

When I run the test the answer is correct in the sense that the code hits 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 likes
2 replies
tisuchi's avatar

@abkrim How about this?


 expect(fn () => AnalyzerCallJobV1::dispatch(
        $ulid,
        $commandCenter,
        $job_time,
        $delayed_at
    ))->throws(CallSlimException::class);
1 like
abkrim's avatar
Level 13

@tisuchi Thanks.

But same problem.

Only can tes directly Action. But can not test featured, from Job.

Ray debug showed me a pass for code but the test:

Exception "App\Exceptions\CallSlimException" not thrown.
at tests/Feature/Jobs/Api/v1/AnalyzerCallJobV1Test.php:116
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:176

Also, I don't pass for Exception class debug.

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Support\Facades\Log;

class CallSlimException extends Exception
{
    public function report(): void
    {
        ray("CallSlimException: {$this->getMessage()}");
        Log::channel('exceptions')->error($this->getMessage());
    }
}

Test for action

Action tests works fine.

it('throws SlimExceptions for server errors', function () use (&$messageError) {
    Http::fake([
        '*' => Http::response('Server error', 500),
    ]);

    $ulid = generateUlid();

    $manufacturer = Manufacturer::factory()->nameSolar()->create();

    $commandCenter = CommandCenter::factory()->create([
        'manufacturer_id' => $manufacturer->id,
    ]);
    
    $job_time = Carbon::now();
    $delayed_at = null;

    $messageError = "SLIM server error 500 [LOGGER][ANALYZER] call $ulid #$commandCenter->id $commandCenter->name"
        . " $job_time";

    CallAnalyzerAction::run($ulid, $commandCenter, $job_time, $delayed_at);
})->throws(CallSlimException::class, $messageError);
1 like

Please or to participate in this conversation.