Search code examples
rubyrspecsidekiq

Rspec: requiring spec/workers


I've got a bunch of Sidekiq workers in app/workers, and some matching specs in spec/workers. How can I add both to my rspec runs? As in stands none are included when I hit up rspec on the Terminal.


Solution

  • I see a few alternatives for your situation:

    • Rspec loading:
      1. Make sure your test files in spec/worker follow the pattern setup for Rspec (e.g. *.rb or *_spec.rb).
      2. Require the worker files in the spec_helper.rb.
    • Manual loading: Require the spec/worker and app/worker files in your spec_helper.rb with a Ruby-defined glob.
    • Sidekiq helper gem: Add the rspec-sidekiq gem to your project, as explained at Sidekiq's.

    To add a bulk of files to the load path, you can either:

    • Add path strings to the $: global variable, early in your boot process (e.g. at the beginning of spec_helper.rb):

    $:.unshift(File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))

    • Use a glob to load the files without modifying the load path:
    Dir[File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))].each do |file|
      require file
    end