Summer Sale! All accounts are 50% off this week.

orest's avatar
Level 13

how to run jobs during testing

I want to test a feature that depends on the database but also uses the queue.

In order to run the test successfully, I need to

  1. Push the job in the queue
  2. Run the job that is pushed in the queue, which will store the results in the table X
  3. Use the results from the table X

Therefore, I don't want to fake the Queue, but instead I want to actually run the jobs that are pushed in the queue.

I use mysql during that specific test and I have set the queue connection to database and I can see that the jobs are pushed in the database.

I've tried to run on a terminal, but it didn't work php artisan queue:work --queue='name of the queue'

0 likes
3 replies
martinbean's avatar
Level 80

@orest You don’t want to run jobs in tests. Break the test up and test the individual components separately.

Have one test that asserts the job is dispatched when it should be (i.e. from a controller or whatever). Then write a test specifically for the job that asserts it creates whatever database records it needs to when it’s ran.

1 like
orest's avatar
Level 13

@martinbean

it turned out that I had another error which was stopping the jobs from running. now it works without even running a worker. I am not sure how and why the jobs are eventually executed.

The reason that i shouldn't run a job during test is because it can potentially slow down the test ? or there is another reason ?

martinbean's avatar

The reason that i shouldn't run a job during test is because it can potentially slow down the test ? or there is another reason ?

@orest Because you should just be testing your code. Dispatching jobs would mean you would need a queue working running, which is hardware configuration. It’s completely out of the scope of a code test.

1 like

Please or to participate in this conversation.