We have two workers as demonstrated below :
class WorkerOne
include Sidekiq::Worker
def perform(arg_1, arg_2)
# some calculation figure out arg_3
WorkerTwo.perform_async(arg_3, arg_2)
end
end
class WorkerTwo
include Sidekiq::Worker
def perform(arg_3, arg_2)
# some heavy lifting
end
end
We need to test that when WorkerOne is executed, WorkerTwo is also enqueued?
We tried using Sidekiq::Testing.inline! to execute WorkerOne.perform_async(arg_1, arg_2)
As WorkerTwo.perform_async(arg_3, arg_2) is executed from inside WorkerOne, we tried testing the WorkerTwo.jobs array length to change from 0 to 1, assuming the job was enqueued from within WorkerOne, but it doesnot change.
We used the below expectation in our test example:
expect{WorkerOne.perform_async(task.id, user.id)}.to change(WorkerTwo.jobs, :size).by(1)
got the below error
expected `Array#size` to have changed by 1, but was changed by 0
Please help with any suggestions and feedbacks on the approach used.
Sidekiq workers can be directly invoked using
Worker.new.performwhich ensures they run in inline mode - simple Ruby method invocation. Not the prettiest solution but comes in handy for testing.Here's an executable gist - https://gist.github.com/tejasbubane/09c1412b88ce1e9cb3dacff0817119d2