'rspec testing carrierwave

I'm making a rails 3.1 app using carrierwave to upload files to aws s3. I've followed the instructions on the carrierwave github repository and am now able to upload files to my aws bucket. It's the testing that got me stuck.

Over the last two days I've been googling and revising, using all the other Q&A's I've found but am finally crying 'mama.' Here's what I've got:

/app/uploaders/image_file_uploader.rb

storage :fog

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

/config/initializers/carrierwave.rb

if Rails.env.test? or Rails.env.cucumber?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end
end

/spec/uploaders/image_file_uploader_spec.rb

require 'spec_helper'
require 'support/fog_helper'
require 'carrierwave/test/matchers'

describe ImageFileUploader do
  include CarrierWave::Test::Matchers

  before do
    ImageFileUploader.enable_processing = true
    @user = Factory(:user, :email => "[email protected]")
    @uploader = ImageFileUploader.new(@user, Factory(:image))
    @uploader.store!(File.open("#{Rails.root}/tmp/uploads/#{Rails.env}/images/"))
  end

  after do
    @uploader.remove!
    ImageFileUploader.enable_processing = false
  end

  context 'the tiny version' do
    it "should scale down a landscape image to be exactly 50 by 50 pixels" do
      @uploader.tiny.should have_dimensions(50, 50)
    end
  end

spec/factories.rb

Factory.define :image do |image|
  include ActionDispatch::TestProcess
  
  image.date_taken            "Sun, 09 Oct 2011"
  image.time_taken            "2000-01-01 03:41:00 UTC"
  image.image_file            fixture_file_upload('spec/support/test_images/audi.png', 'image/png')
  image.taken_by              "John Doe"
  image.collection            "N/A"
  image.comments              "Beautiful day!"
  image.association   :user

end

While my /public/uploads/tmp/ is getting filled up with 'tiny' (and other versions) of the images whose generation I am testing, the tests continue to fail with the following error message:

  1. ImageFileUploader the tiny version should scale down a landscape image to be exactly 50 by 50 pixels
     Failure/Error: @uploader = ImageFileUploader.new(@user, Factory(:image))
     Excon::Errors::NotFound:
       Expected(200) <=> Actual(404 Not Found)
         request => {:expects=>200}
         response => #<Excon::Response:0x0000010569f928 @body="", @headers={}, @status=404>
     # ./spec/uploaders/image_file_uploader_spec.rb:11:in `block (2 levels) in <top (required)>'

I know that the above means that rspec isn't finding my testing bucket. Anyone have any thoughts about what I'm doing wrong?

would be super grateful for any new leads.

UPDATE: 10/11/11 The file upload works but I've stalled in figuring out how to get test involving the images to pass. In the short term, I will use a placeholder image as I flesh out the rest of my app and return to this later. I'll post a further update once I figure this out. (Do leave any thoughts if you have any insights, however.)



Solution 1:[1]

Have you tried this?

  1. In /app/uploaders/image_file_uploader.rb remove storage :fog

  2. In /config/initializers/carrierwave.rb add a similar configuration block for production as you have for test and cucumber, and set config.storage = :fog there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakob W