lundi 8 juin 2015

Fix spurious rspec "photo can't be blank" failures for Carrierwave

I have a Rails 3 app that uses rspec for testing and Carrierwave for image uploads. Right now, we have a Profile model that has several image fields, for example: photo1 and photo2.

My rspec tests will fail sporadically (usually on the CI server, but occasionally on my local machine) with an error similar to the following:

 Failure/Error: let(:profile) { create(:profile) }
  ActiveRecord::RecordInvalid:
    Validation failed: Image can't be blank

The Profile model has images sourced from Rails.root.join, for example:

IMAGE_1 ||= File.open(Rails.root.join('spec/fixtures/p1.jpg'))
IMAGE_2 ||= File.open(Rails.root.join('spec/fixtures/p2.jpg'))

FactoryGirl.define do
  factory :profile do
    ...
    photo1 IMAGE_1
    photo2 IMAGE_2
    ...
  end
end

Because these spurious failures occur sporadically and are not easily reproducible, I think the reason this is happening is that sometimes the images are not "loaded" fast enough when the tests are occuring. They usually occur on the build or create methods in the spec files, since that is when validations are tested.

How can this architecture/code be changed such that the tests pass? I've taken a look at this SO answer which proposes using a NullStorage class that implements the minimum interface for a storage provider. However, while this solution does technically fix the incorrect failures, it means that every method used on the images must be stubbed out, which is tedious. Are other people using that solution, or is there a better way?

Aucun commentaire:

Enregistrer un commentaire