svelandiag
svelandiag

Reputation: 4320

Write a feature Test using capybara when using sidekiq `perform_in` method

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.

Upvotes: 0

Views: 991

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49890

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

Upvotes: 2

Oscar
Oscar

Reputation: 52

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)

Upvotes: 0

Related Questions