Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby
Branch: master
Pull request Compare This branch is 101 commits behind philostler:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitattributes
.gitignore
.rspec
.simplecov
.travis.yml
CHANGES.md
Gemfile
LICENSE
README.md
rspec-sidekiq.gemspec

README.md

RSpec for Sidekiq

RubyGems Code Climate Travis CI Coveralls Gemnasium coderwall

Simple testing of Sidekiq jobs via a collection of matchers and helpers

RubyGems | Code Climate | GitHub | Travis CI | Coveralls | Gemnasium | RubyDoc | Ruby Toolbox

Jump to Matchers » | Jump to Helpers »

Installation

# Gemfile
group :test do
  gem "rspec-sidekiq"
end

rspec-sidekiq requires sidekiq/testing by default so there is no need to include the line require "sidekiq/testing" inside your spec_helper.rb.

IMPORTANT! This has the effect of not pushing enqueued jobs to Redis but to a job array to enable testing (see the FAQ & Troubleshooting Wiki page). Thus, only include gem "rspec-sidekiq" in environments where this behaviour is required, such as the test group.

Configuration

If you wish to modify the default behaviour, add the following to your spec_helper.rb file

RSpec::Sidekiq.configure do |config|
  # Clears all job queues before each example
  config.clear_all_enqueued_jobs = true # default => true

  # Whether to use terminal colours when outputting messages
  config.enable_terminal_colours = true # default => true

  # Warn when jobs are not enqueued to Redis but to a job array
  config.warn_when_jobs_not_processed_by_sidekiq = true # default => true
end

Matchers

be_delayed

Describes a method that should be invoked asynchronously (See Sidekiq Delayed Extensions)

Object.delay.is_nil? # delay
expect(Object.method :is_nil?).to be_delayed
Object.delay.is_a? Object # delay with argument
expect(Object.method :is_a?).to be_delayed(Object)

Object.delay_for(1.hour).is_nil? # delay for
expect(Object.method :is_nil?).to be_delayed.for 1.hour
Object.delay_for(1.hour).is_a? Object # delay for with argument
expect(Object.method :is_a?).to be_delayed(Object).for 1.hour

Object.delay_until(1.hour.from_now).is_nil? # delay until
expect(Object.method :is_nil?).to be_delayed.until 1.hour.from_now
Object.delay_until(1.hour.from_now).is_a? Object # delay until with argument
expect(Object.method :is_a?).to be_delayed(Object).until 1.hour.from_now

be_processed_in

Describes the queue that a job should be processed in

sidekiq_options queue: :download
# test with...
expect(AwesomeJob).to be_processed_in :download # or
it { should be_processed_in :download }

be_retryable

Describes if a job should retry when there is a failure in it's execution

sidekiq_options retry: 5
# test with...
expect(AwesomeJob).to be_retryable true # or
it { should be_retryable true }
# ...or alternatively specifiy the number of times it should be retried
expect(AwesomeJob).to be_retryable 5 # or
it { should be_retryable 5 }
# ...or when it should not retry
expect(AwesomeJob).to be_retryable false # or
it { should be_retryable false }

be_unique

Describes when a job should be unique within it's queue

sidekiq_options unique: true
# test with...
expect(AwesomeJob).to be_unique
it { should be_unique }

have_enqueued_job

Describes that there should be an enqueued job with the specified arguments

Awesomejob.perform_async "Awesome", true
# test with...
expect(AwesomeJob).to have_enqueued_job("Awesome", true)

have_enqueued_jobs

Removed. See the FAQ & Troubleshooting Wiki page for alternative and more information

Example matcher usage

require "spec_helper"

describe AwesomeJob do
  it { should be_processed_in :my_queue }
  it { should be_retryable 5 }
  it { should be_unique }

  it "enqueues another awesome job" do
    subject.perform

    expect(AnotherAwesomeJob).to have_enqueued_job("Awesome", true)
  end
end

Helpers

Batches

If you are using Sidekiq Batches (Sidekiq Pro feature), rspec-sidekiq replaces the implementation (using the NullObject pattern) enabling testing without a Redis instance. Mocha and RSpec stubbing is supported here.

within_sidekiq_retries_exhausted_block

sidekiq_retries_exhausted do |msg|
  bar('hello')
end
# test with...
FooClass.within_sidekiq_retries_exhausted_block {
  expect(FooClass).to receive(:bar).with("hello")
}

Testing

bundle exec rspec spec

Contribute

Please do! If there's a feature missing that you'd love to see then get in on the action!

Issues/Pull Requests/Comments all welcome...

Something went wrong with that request. Please try again.