Class: RuboCop::Cop::RSpec::SidekiqPerformMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/sidekiq_perform_matcher.rb

Overview

Prevent the use of ‘receive(:perform_async)` matcher to use instead rspec-sidekiq matcher like `enqueue_sidekiq_job`.

Examples:

# bad
expect(MyWorker).to receive(:perform_async).with(args)
expect(MyWorker).to receive(:perform_in).with(5.seconds, args)
expect(MyWorker).to receive(:perform_at).with(specific_time, args)
expect(MyWorker).to receive(:perform_bulk)

# good
expect(MyWorker).to have_enqueued_sidekiq_job.with(args)
expect(MyWorker).to have_enqueued_sidekiq_job.in(1.seconds).with(args)
expect(MyWorker).to have_enqueued_sidekiq_job.at(specific_time).with(args)
expect(MyWorker).to have_enqueued_sidekiq_job

Constant Summary collapse

MSG =
'Use `have_enqueued_sidekiq_job` instead of `receive(:perform_%s)`.'
RESTRICT_ON_SEND =
i[receive].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/sidekiq_perform_matcher.rb', line 31

def on_send(node)
  return unless perform_matcher?(node)

  perform_method = node.first_argument.value.to_s.sub('perform_', '')

  add_offense(node, message: format(MSG, perform_method))
end

#perform_matcher?(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/rspec/sidekiq_perform_matcher.rb', line 27

def_node_matcher :perform_matcher?, "(send nil? :receive (sym {:perform_async :perform_in :perform_at :perform_bulk}))\n"