wspurgin / rspec-sidekiq

RSpec for Sidekiq
https://github.com/wspurgin/rspec-sidekiq
Other
662 stars 133 forks source link

Block syntax `enqueue_sidekiq_job` matcher #170

Closed pirj closed 1 year ago

pirj commented 3 years ago

There is a slight problem with the existing have_enqueued_sidekiq_job matcher, specifically that it's prone to the incidental state error, unlike rspec-rails's Active Job-oriented have_enqueued_job that is a block syntax matcher. E.g.:

class Model < ActiveRecord::Base
  after_save :schedule_job

  def foo!
    # I do nothing!
  end

  private

  def schedule_job
    AwesomeJob.perform_async('hello')
  end
end
RSpec.describe Model do
  describe '#foo!' do
    subject!(:model) { Model.create! }

    it 'schedules AwesomeJob' do
      model.foo!
      expect(AwesomeJob).to have_enqueued_sidekiq_job('hello')
    end
  end
end

In the above example, the job was scheduled. But it would be scheduled no matter if model.foo! was called or not.

I suggest adding a block-syntax enqueue_sidekiq_job matcher that would work similarly to rspec-rails's one:

RSpec.describe Model do
  describe '#foo!' do
    subject!(:model) { Model.create! }

    it 'schedules AwesomeJob' do
      expect { model.foo! }
        .to enqueue_sidekiq_job(AwesomeJob).with('hello')
    end
  end
end

I can handle adding it if you have no objections on the idea.

pirj commented 3 years ago

Ping

sineed commented 3 years ago

hey @pirj have you worked on this one? I like the idea and I'm curious if I can sneak the implementation :)

pirj commented 3 years ago

I haven't yet, but since you support the idea, I'll tackle it. For the implementation, I think of something very similar to this.

sineed commented 3 years ago

Yes, good to add negated matcher as well. It will be quite handy and readable to see something like:

expect { subject }
  .to have_enqueued_sidekiq_job(FooJob)
  .and have_enqueued_sidekiq_job(BarJob)
  .and avoid_enqueuing_sidekiq_job(BazJob)
pirj commented 3 years ago

Released as https://github.com/pirj/rspec-enqueue_sidekiq_job https://rubygems.org/gems/rspec-enqueue_sidekiq_job

I've closed the PR. Please feel free to close the ticket as well.

wspurgin commented 1 year ago

I'd like this in rspec-sidekiq and I've added it as a goal for v4. If you're still interested @pirj (I know it's been several years), feel free to re-open a PR. Otherwise I'll try and add a block syntax version using the same style expect { }.to enqueue_sidekiq_job(...)

pirj commented 1 year ago

I ended up creating https://github.com/pirj/rspec-enqueue_sidekiq_job Since we don’t use Sidekiq directly in my current project, I’m not so interested in actively maintaining the repo further. I’ll be happy if you take it over, borrow its code, or just include the gem as a runtime dependency, whatever you feel is right.

And thanks for the gem!

wspurgin commented 1 year ago

Thanks @pirj 👍 Appreciate it.