1

I'm saving an object to database in RSpec. This object has a Carrierwave field with its presence validation. I'm using fixture_file_upload for that field in a factory and it's very slow. I would like to stub Carrierwave file handling completely, but all solutions seem outdated and I can't get them to work.

My model:

class Product < ActiveRecord::Base
  validates_presence_of   :image
  mount_uploader :image,  ProductMainImageUploader
end

My factory:

factory :product do
  name          { Faker::Product.product_name }
  slug          { name ? name.parameterize : nil }
  image         { 'sample_image.jpg' }
end

My spec:

describe Product do
  it "creates record" do
    ProductMainImageUploader.any_instance.stub(:store!)
    ProductMainImageUploader.any_instance.stub(:store_image!)
    product = create(:product)
  end
end

Results:

  1) Product creates record
     Failure/Error: product = create(:product, name: 'heyo')
     ActiveRecord::RecordInvalid:
       Validation failed: Image can't be blank
     # ./spec/models/product_spec.rb:26:in `block (2 levels) in <top (required)>'

The stubs have no effect at all. I'm using Ruby 2.1.0. What can I do to fix this?

leemour
  • 11,414
  • 7
  • 36
  • 43

1 Answers1

0

Try stubbing image

before do
  Product.stub(:image).and_return(ProductMainImageUploader.new)
end
lsaffie
  • 1,764
  • 1
  • 17
  • 22