Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 test fixtures with carrierwave?

I'm working on upgrading from attachment_fu to carrierwave, since attachment_fu is broken in rails 3.

None of the tests are able to run, because we have invalid fixtures that were using the syntax from attachment_fu for attachment files.

For example, we have a Post model that has one PostAttachment. Here's what the data in the PostAttachment fixture looks like:

a_image:
  post_id: 1
  attachment_file: <%= Rails.root>/test/files/test.png

And this is the error I'm getting:

ActiveRecord::StatementInvalid: PGError: ERROR:  column "attachment_file" of relation "post_attachments" does not exist
LINE 1: INSERT INTO "post_attachments" ("post_id", "attachment_file"...

attachment_file would have been picked up by attachment_fu, and it would have taken care of all the processing to create the attachment_fu attachment for the model.

Is there a way to have image attachments in the fixtures, but with using CarrierWave instead?

like image 303
keithepley Avatar asked Sep 23 '11 20:09

keithepley


4 Answers

The only way I've managed to get this to work is to use a storage provider specifically for testing that doesn't actually save/read files.

In your config/initializers/carrier_wave.rb Add a NullStorage class that implements the minimum interface for a storage provider.

# NullStorage provider for CarrierWave for use in tests.  Doesn't actually
# upload or store files but allows test to pass as if files were stored and
# the use of fixtures.
class NullStorage
  attr_reader :uploader

  def initialize(uploader)
    @uploader = uploader
  end

  def identifier
    uploader.filename
  end

  def store!(_file)
    true
  end

  def retrieve!(_identifier)
    true
  end
end

Then when initializing CarrierWave add a clause for the test environment, e.g.,

if Rails.env.test?
    config.storage NullStorage
end

Here is a gist of my complete carrier_wave.rb for reference. It also includes how to setup S3 for uploads in staging/production and local storage for development so you can see how to configure CarrierWave in context.

Once CarrierWave is configured you can simply put any string in the fixtures column to simulate an uploaded file.

like image 56
Gerry Shaw Avatar answered Oct 19 '22 11:10

Gerry Shaw


Try passing a file instead of a String.

a_image:
    post_id: 1
    attachment_file: File.open(Rails.root.join("test/files/test.png"))

This works for me using FactoryGirl

Note: Edit thanks to @dkobozev

like image 9
e3matheus Avatar answered Oct 19 '22 11:10

e3matheus


config/initializers/carrier_wave.rb

In Rails 4

# class NullStorage is defined here before the following block

if Rails.env.test?
  CarrierWave.configure do |config|
    config.storage NullStorage
  end
end

& in fixtures:

a_image:
  post_id: 1
  attachment_file: <%= File.open(Rails.root.join("test/files/test.png")) %>
like image 2
Serjik Avatar answered Oct 19 '22 13:10

Serjik


To be able to use fixtures that have uploaded files as well as doing uploads in the tests, I've played around with CarrierWave for a bit lately. I've written an article about how I'd do it.

like image 1
jkreeftmeijer Avatar answered Oct 19 '22 13:10

jkreeftmeijer