Laravel Tutorials

Test Job Failures Precisely with Laravel's assertFailedWith Method

Published
Test Job Failures Precisely with Laravel's assertFailedWith Method image

Laravel introduces the assertFailedWith method for more precise testing of queued job failures, enabling developers to verify exactly how and why jobs fail rather than just confirming failure occurred.

The assertFailedWith method supports multiple assertion types for comprehensive job failure testing:

use App\Jobs\ProcessDocument;
use App\Exceptions\ProcessingException;

$job = (new ProcessDocument)->withFakeQueueInteractions();

// Test with message
$job->assertFailedWith('processing failed');

// Test with exception class
$job->assertFailedWith(ProcessingException::class);

// Test with exception instance
$job->assertFailedWith(new ProcessingException);

// Test with specific message
$job->assertFailedWith(new ProcessingException(message: 'invalid format'));

// Test with message and code
$job->assertFailedWith(new ProcessingException(message: 'timeout error', code: 408));

It could also be used for integration testing with external services:

class ExternalServiceJobTest extends TestCase
{
    public function test_api_sync_handles_rate_limiting()
    {
        Http::fake(['*' => Http::response([], 429)]);
        
        $job = new SyncExternalDataJob($apiEndpoint)
            ->withFakeQueueInteractions();
            
        $job->handle();
        
        $job->assertFailedWith(
            new RateLimitException('API rate limit exceeded', 429)
        );
    }
    
    public function test_webhook_delivery_handles_invalid_endpoint()
    {
        $job = new DeliverWebhookJob('invalid-url', $payload)
            ->withFakeQueueInteractions();
            
        $job->handle();
        
        $job->assertFailedWith(
            new InvalidEndpointException('Invalid webhook URL format')
        );
    }
}

The assertFailedWith method enables comprehensive job failure testing by verifying specific failure conditions, making test suites more reliable and helping developers ensure proper error handling in their queue workers.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Load Eloquent Models From Form Request Input in Laravel image

Load Eloquent Models From Form Request Input in Laravel

Read article
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Read article
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article