mind.blank
mind.blank

Reputation: 4880

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)

Upvotes: 20

Views: 37978

Answers (3)

Unkas
Unkas

Reputation: 3708

Sidekiq provides a few options for testing your workers

https://github.com/mperham/sidekiq/wiki/Testing

For me it worked after I add these lines at the top of a spec-file

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

Upvotes: 4

Mike Perham
Mike Perham

Reputation: 22238

Read the Testing page in the Sidekiq wiki.

Upvotes: 16

Phil Ostler
Phil Ostler

Reputation: 447

Take a look at the rspec-sidekiq gem I've authored to do exactly this (testing of Sidekiq workers).

Upvotes: 10

Related Questions