My Sidekiq workers are like the example below:
Class BooksWorker
include Sidekiq::Worker
def perform
books = Book.where(collected: true)
books.each do |book|
book.update_attribute(:status, "read")
book.toggle!(:collected)
end
end
end
I would like to use Sidekiq testing in disable mode to check that:
- jobs are enqueued
- Sidekiq communicates with Redis (jobs are pushed to Redis)
- jobs are retrieved from Redis and executed
- jobs provide the expected results
What kind of test can I create to check all the four points above?
Consider the example test below:
require 'test_helper'
require 'sidekiq/testing'
class BooksWorkerDisableTest < Minitest::Test
def setup
configure = -> (config) do
config.redis = { url: 'redis://localhost:6379/15' }
end
Sidekiq.configure_client(&configure)
Sidekiq.configure_server(&configure)
Sidekiq::Testing.disable!
@books = Book.where(collected: true)
end
test "collected books should be marked as read before archived" do
BooksWorker.perform_async
@books.each do |book|
assert book.status == "read"
assert book.collected == false
end
end
end
If Sidekiq during the test enqueues the job, pushes to Redis, retrieves it from Redis and performs it, how long does it take for the test to be completed, assuming that the test is closed only after the job is performed?
The above test checks only the fourth point: how can I test the other points? I suppose it is only implicit that, if the jobs are performed, then they were already queued and pushed to Redis before.