Don't understand why Sidekiq tests are failing with Testing::inline! when others wont

766 views Asked by At

I am about to write tests for a Imaage Worker. I don't understand why some of the tests are failing when using Sidekiq::Testing.inline! when others with the same structure wont.

Can someone explain please

  • why the first test fails WITH Sidekiq::Testing.inline!
  • why the second test fails WITHOUT Sidekiq::Testing.inline!
class ProductImageJob
  include Sidekiq::Job

  sidekiq_options retry: 3

  def perform(product_id, enable_after_update = true)
    product = Product.unscoped.find(product_id)

    if product.generate_variants
      product.mark_visible! if enable_after_update
      logger.info "Images for product #{product_id} generated"
    else
      raise StandardError, "Product images for ID #{product_id} could not be generated"
    end

  end
end

# rails_helper.rb
RSpec.configure do |config|
  config.before(:each) do
    Sidekiq::Worker.clear_all
  end
end

# spec/jobs/product_image_job.rb
require 'rails_helper'

RSpec.describe ProductImageJob do

  let(:product) { Fabricate :product }

  describe 'perform' do

    it 'should call generate_variants' do
      # Sidekiq::Testing.inline! # when UNcommented: FAILS

      expect_any_instance_of(Product).to receive(:generate_variants)

      ProductImageJob.perform_async(product.id)
    end

    it 'should receive mark_visible! if job done' do
      Sidekiq::Testing.inline! # when COMMENTED FAILS
      # allow_any_instance_of(Product).to receive(:generate_variants).and_return(true)
      expect_any_instance_of(Product).to receive(:mark_visible!)

      ProductImageJob.perform_async(product.id)
    end
  end

end

I also tried to mock the generate_variants as you can see, but that does not change it's behaviour.

Both tests are generally doing the same thing, why is the one failing and the others not, and vice versa?

0

There are 0 answers