Skip to content

Conversation

@gdebrauwer
Copy link
Contributor

When you use afterResponse() on a job in route and you want to test that job, then there is currently no easy way to do this because the job will never be executed in the test.

/** @test */ public function some_job() { dispatch(function () { // will not be executed when running the test })->afterResponse(); }

This PR fixes that by providing an option to disable (similar to withoutDefer)

/** @test */ public function some_job() { Bus::disableDispatchingAfterResponse(); dispatch(function () { // will be executed when running the test })->afterResponse(); }
@taylorotwell taylorotwell merged commit 6f55dbc into laravel:12.x Apr 24, 2025
58 checks passed
@christophrumpel
Copy link
Contributor

Thanks @gdebrauwer, nice addition. The closure worked nicely for me.

Should this also work for jobs dispatched like this: CreateUserJob::dispatchAfterResponse();
Because it did not work for this test of mine:

it('creates a user', function () { // Arrange Bus::withoutDispatchingAfterResponses(); // Act resolve(CreateUserAction::class); // Assert assertDatabaseCount(User::class, 1); });
@gdebrauwer
Copy link
Contributor Author

@christophrumpel Yes, it should also work with 'dispatchAfterResponse' as that method is just a wrapper around 'afterResponse()':

// In Illuminate/Foundation/Bus/Dispatchable.php public static function dispatchAfterResponse(...$arguments) { return self::dispatch(...$arguments)->afterResponse(); }

In your example, you are resolving the 'CreateUserAction' class but don't call any methods on it; that may be the issue I think

@christophrumpel
Copy link
Contributor

Oh wow 😬 Thanks a lot, that was it :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants