Skip to content

How to: Use test fixtures

allspiritseve edited this page Apr 2, 2012 · 7 revisions

Whereas other gems such as Paperclip allow you to sham mounted uploaders as Strings, CarrierWave requires actual files. Here is an example blueprints.rb file:

# Machinist example
Sham.image { File.open("#{Rails.root}/test/fixtures/files/rails.png") }
User.blueprint do
  avatar { Sham.image }
end

EDIT:
I belive StringIO's also work

Attaching a file to a FactoryGirl object is pretty much identical.

# FactoryGirl example
Factory.define :brand do |f|
  f.name "My Brand"
  f.description "Foo"
  f.logo { File.open(File.join(Rails.root, 'spec', 'support', 'brands', 'logos', 'logo_image.jpg')) }
end

AFAICT the curly braces are required to make FactoryGirl attach it lazily, i.e. whenever the factory is built.

Attaching a file to a Fabrication object has to be done in an after_create block.

# Fabrication example
Fabricator(:brand) do
  name "My Brand"
  description "Foo"
  after_create { |brand| brand.file = File.open(File.join(Rails.root,'spec','support','brands','logos','logo_image.jpg')) } # has to exist
end
Clone this wiki locally