0

Most stuff I can find related is about testing that the background job is scheduled like this question.

However, I need to write a feature test that actually waits for the execution of the background job, so let's say:

BatchWorker.perform_in(30.minutes)

I will need to write a feature test that makes some assertions based on the results of this job that will execute in 30 minutes. How to do that?

UPDATE

Just to clarify... I don't need to test if the job is enqueued, for example with rspec-sidekiq gem you can do something like

Awesomejob.perform_in 30.minutes, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(30.minutes)

This is not what I need... I need to make an assertion on the page with capybara after the execution of the job.

svelandiag
  • 4,231
  • 1
  • 36
  • 72

2 Answers2

2

I'm assuming you don't actually want to wait the 30 minutes during your test, and instead want to execute the job immediately (otherwise you just sleep for 30 minutes - but really doesn't gain you anything). The Sidekiq adapter has a number of methods to help with this such as Sidekiq::Testing.fake! and Sidekiq::Testing.inline!. In your case it sounds like for this test you'd want to use inline! in order to have the jobs executed immediately.

require "sidekiq/testing"
Sidekiq::Testing.inline!

If you used fake! then you'd need to call drain_all in order to have your enqueued jobs be executed. You can read more about this at https://sloboda-studio.com/blog/testing-sidekiq-jobs/ or in the sidekiq wiki and code at https://github.com/mperham/sidekiq/wiki/Testing and https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/testing.rb

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thanks! it worked! it seems the easiest is adding `Sidekiq::Testing.inline!` to a `before` block in the spec file. – svelandiag Apr 27 '21 at 16:18
0

You can checkout rspec-sidekiq

Awesomejob.perform_in 30.minutes, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(30.minutes)
Oscar
  • 52
  • 3
  • But this is testing if the job is enqueued, this is not what Im looking for. I need to check something in the page with capybara after the execution of the job... – svelandiag Apr 27 '21 at 14:46