Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec and carrierwave - how set an image url directly (BYPASS carrierwave uploader during testing)

I'm writing some rspec specs where an object's widgetpic field needs to point to an actual image, but without using the carrierwave uploader.

When in test, I just want to bypass the uploader and manually set a test picture's url. Specifically I have a few test images online and simply want to have foo.widgetpic.url to return some fixed location:"http://mybucket.s3.amazonaws.com/test_pix/test1.png"

The Widget model has:

mount_uploader :widgetpic, WidgetPicUploader

So in my FactoryGirl factory I assume I need to do something like this

trait :test_pic do
   SOMETHING "http://mybucket.s3.amazonaws.com/test_pix/test1.png"
end

I tried setting widgetpic_url and remote_widgetpic_url but that didn't work (widgetpic.url still returns my default 'no image' image defined in my uploader).

like image 366
jpw Avatar asked Oct 22 '22 05:10

jpw


1 Answers

It appears that something else is broken about your uploader, not the factory. Can you post your carrierwave configuration?

That aside, the correct factory syntax for remote images is remote_< resource >_url:

trait :test_pic do
  remote_widgetpic_url "http://mybucket.s3.amazonaws.com/test_pix/test1.png"
end

However, be aware that by doing this, CarrierWave is not being bypassed. CarrierWave will fetch the remote image for you and use the downloaded image to re-upload to the location specified in your uploader. You can use this technique in conjunction with something like WebMock or VCR to emulate and/or record the HTTP request and play it back.

A simpler approach might be to use the mocking capabilities built into Fog to simulate the upload for the test. That way, you can just place a dummy image in your factory:

factory :widget do   
  # ...

  widgetpic Rack::Test::UploadedFile.new(File.open(
    File.join(
      File.dirname(__FILE__), '../fixtures/images/your_image_here.png')))
end

That image will be used to simulate the upload, but will not be posted to the location on S3 and you can test the upload from end-to-end.

like image 150
Faun Avatar answered Oct 27 '22 22:10

Faun