Random results when trying to set an attachment with ActiveStorage

75 views Asked by At

I have the following in my test:

...

let(:image) { Rack::Test::UploadedFile.new('spec/support/assets/test.png', 'image/png') }
subject { described_class.new(gold_bar_order).set_bar_custom_image(image) }

it do
  subject

  expect(gold_bar_order.reload.bar_custom_image.present?).to be_truthy
end

And here is the class from the method I'm testing:

class GoldBarOrderCustomizationUpdater
  def initialize(gold_bar_order)
    @gold_bar_order = gold_bar_order
  end

  def set_bar_custom_image(bar_custom_image)
    update_values do 
      @gold_bar_order.bar_custom_image.attach(bar_custom_image)
      @gold_bar_order.save! 
    end
  end

  ...

  private

  def update_values
    ActiveRecord::Base.transaction do
      yield

      calculator = MasterOrderCalculator.new(@gold_bar_order.master_order)

      calculator.recalculate_sub_orders!
      calculator.save_values!
    end
  end
end

What is happening is that I'm receiving random results for the described test: if I run it a given number of times, it fails or passes without particular order.

I couldn't understand why. Any guesses?

0

There are 0 answers