Daniel-Pablo's avatar

How to test a job?

Hello Laracaster, how I can test this???

it('dispatch and user and change his name', function () {
    UserName::dispatch($this->user, 'Andrew');
    $newName = $this->user->refresh()->name;
    expect($newName)->toBe("Andrew");
});

So far it works, but this is the thing I can't wrap my head on

1- I can't work on my TEST-DB, I got to work in my official DB thus Redis is pointing there... so that's an issue

2- when I run the test the first time, it gives an error, because it dispatches the job, and continues the execution, so on the second run of course it passes because Redis already took the job a make the change of the name... so, what I want is to test this the right way it should... hope this makes sense, on execution know that this pass and do the magic... how to, thanks in advance

0 likes
1 reply
Daniel-Pablo's avatar
Daniel-Pablo
OP
Best Answer
Level 12

For days I search and finally It was revealed to me

it('can change his name to the one of the dispatched job', function () {
    $user = User::factory()->create();
    TestRedisJobQueueWithUser::dispatchSync($user);
    expect($user->refresh())
        ->name->toBe('JobChangeThis');
});

Hope this illuminates someone's lost

Please or to participate in this conversation.