Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test background jobs (Sidekiq) with Rspec?

A large part of my application relies on background jobs to process user's websites, so I need to write some tests to cover these.

First question is how to test code that's in a Sidekiq worker class, for example app/workers/some_worker.rb

class SomeWorker
  include Sidekiq::Worker

  def perform(model_id)
    ...
  end
end

Secondly, how do I stub code such as the following so I don't need to actually run workers and slow down my tests?

response = HTTParty.get("http://users-site.com", timeout: 30)
like image 881
mind.blank Avatar asked Mar 30 '13 10:03

mind.blank


People also ask

How do you test for Sidekiq jobs?

To test your Sidekiq Worker jobs array, run WorkerNameHere.jobs in terminal and see if it contains your job with your jid. If it does, then it was enqueued in Sidekiq to be run as a job.

How do I run Sidekiq in the background?

To run sidekiq, you will need to open a terminal, navigate to your application's directory, and start the sidekiq process, exactly as you would start a web server for the application itself. When the command executes you will see a message that sidekiq has started.

How do I test Sidekiq locally?

On the browser, goto ==> http://localhost:{port}/sidekiq/recurring-jobs . where {port} is the port your application is running in. You will see the list of scheduled jobs for your application and some other details about it. Show activity on this post.


1 Answers

Read the Testing page in the Sidekiq wiki.

like image 168
Mike Perham Avatar answered Oct 05 '22 20:10

Mike Perham