Factory with carrierwave upload field

Petya petrov picture Petya petrov · May 13, 2011 · Viewed 12k times · Source

Hello i need to build up Factory for my model, for example

Factory.define :farm do |f|
  f.name { Factory.next :name }
  f.harvest '3'
  f.offers 'Random'
  f.latitude '43'
  f.longitude '-70'
  f.about 'We rocks!'
  f.logo { Factory.next :logo } # this doesn't work
end

For now im just pass string "#{n}.jpg" into my logo field and this dont work, how to evalute this field? Im using CarrierWave for uploading.

Answer

Adrian Macneil picture Adrian Macneil · Dec 23, 2011

You need to include ActionDispatch::TestProcess in your factory (Rails 3), and use the fixture_file_upload helper:

include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :image do
    title "Example image"
    file { fixture_file_upload("files/example.jpg", "image/jpeg") }
  end
end

Also be sure to create an example file at test/fixtures/files/example.jpg, otherwise you will get an error that the file does not exist.