Skip to content

Repository files navigation

Welcome @packrat386 as new maintainer for rspec-sidekiq!

RSpec for Sidekiq

RubyGemsCode ClimateTravis CICoverallsGemnasium

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

# Gemfilegroup:testdogem'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.configuredo |config|
# Clears all job queues before each exampleconfig.clear_all_enqueued_jobs=true# default => true# Whether to use terminal colours when outputting messagesconfig.enable_terminal_colours=true# default => true# Warn when jobs are not enqueued to Redis but to a job arrayconfig.warn_when_jobs_not_processed_by_sidekiq=true# default => trueend

Matchers

be_delayed

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

Object.delay.is_nil?# delayexpect(Object.method:is_nil?).tobe_delayedObject.delay.is_a?Object# delay with argumentexpect(Object.method:is_a?).tobe_delayed(Object)Object.delay_for(1.hour).is_nil?# delay forexpect(Object.method:is_nil?).tobe_delayed.for1.hourObject.delay_for(1.hour).is_a?Object# delay for with argumentexpect(Object.method:is_a?).tobe_delayed(Object).for1.hourObject.delay_until(1.hour.from_now).is_nil?# delay untilexpect(Object.method:is_nil?).tobe_delayed.until1.hour.from_nowObject.delay_until(1.hour.from_now).is_a?Object# delay until with argumentexpect(Object.method:is_a?).tobe_delayed(Object).until1.hour.from_now#Rails MailerMyMailer.delay.some_mailexpect(MyMailer.instance_method:some_mail).tobe_delayed

be_processed_in

Describes the queue that a job should be processed in

sidekiq_optionsqueue: :download# test with...expect(AwesomeJob).tobe_processed_in:download# orit{is_expected.tobe_processed_in:download}

be_retryable

Describes if a job should retry when there is a failure in its execution

sidekiq_optionsretry: 5# test with...expect(AwesomeJob).tobe_retryabletrue# orit{is_expected.tobe_retryabletrue}# ...or alternatively specify the number of times it should be retriedexpect(AwesomeJob).tobe_retryable5# orit{is_expected.tobe_retryable5}# ...or when it should not retryexpect(AwesomeJob).tobe_retryablefalse# orit{is_expected.tobe_retryablefalse}

save_backtrace

Describes if a job should save the error backtrace when there is a failure in its execution

sidekiq_optionsbacktrace: 5# test with...expect(AwesomeJob).tosave_backtrace# orit{is_expected.tosave_backtrace}# ...or alternatively specifiy the number of lines that should be savedexpect(AwesomeJob).tosave_backtrace5# orit{is_expected.tosave_backtrace5}# ...or when it should not save the backtraceexpect(AwesomeJob).to_notsave_backtrace# orexpect(AwesomeJob).tosave_backtracefalse# orit{is_expected.to_notsave_backtrace}# orit{is_expected.tosave_backtracefalse}

be_unique

Describes when a job should be unique within its queue

sidekiq_optionsunique: true# test with...expect(AwesomeJob).tobe_uniqueit{is_expected.tobe_unique}

be_expired_in

Describes when a job should expire

sidekiq_optionsexpires_in: 1.hour# test with...it{is_expected.tobe_expired_in1.hour}it{is_expected.to_notbe_expired_in2.hours}

have_enqueued_sidekiq_job

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

Note: When using rspec-rails >= 3.4, use have_enqueued_sidekiq_job instead to prevent a name clash with rspec-rails' ActiveJob matcher.

AwesomeJob.perform_async'Awesome',true# test with...expect(AwesomeJob).tohave_enqueued_sidekiq_job('Awesome',true)# Code written with older versions of the gem may use the deprecated# have_enqueued_job matcher.expect(AwesomeJob).tohave_enqueued_job('Awesome',true)

Testing scheduled jobs

Use chainable matchers #at and #in

time=5.minutes.from_nowAwesomejob.perform_attime,'Awesome',true# test with...expect(AwesomeJob).tohave_enqueued_sidekiq_job('Awesome',true).at(time)
Awesomejob.perform_in5.minutes,'Awesome',true# test with...expect(AwesomeJob).tohave_enqueued_sidekiq_job('Awesome',true).in(5.minutes)

Example matcher usage

require'spec_helper'describeAwesomeJobdoit{is_expected.tobe_processed_in:my_queue}it{is_expected.tobe_retryable5}it{is_expected.tobe_unique}it{is_expected.tobe_expired_in1.hour}it'enqueues another awesome job'dosubject.performexpect(AnotherAwesomeJob).tohave_enqueued_sidekiq_job('Awesome',true)endend

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_exhausteddo |msg|
bar('hello')end# test with...FooClass.within_sidekiq_retries_exhausted_block{expect(FooClass).toreceive(:bar).with('hello')}

Testing

bundle exec rspec spec

Maintainers

  • @packrat386
  • @philostler

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...

Releases

Packages

Contributors

Languages