120

I have model Person that has many Images, where images has a Paperclip attachment field called data, an abbreviated version displayed below:

class Person
  has_many :images
  ...
end

class Image
  has_attached_file :data
  belongs_to :person
  ...
end

Person is required to have at least one Image attached to it.

When using FactoryGirl, I have code akin to the following:

Factory.define :image do |a|
  a.data { File.new(File.join(Rails.root, 'features', 'support', 'file.png')) }
  a.association :person
end

Factory.define :person do |p|
  p.first_name 'Keyzer'
  p.last_name 'Soze'
  p.after_create do |person|
    person.assets = [Factory.build(:image, :person => person)]
  end
  # p.images {|images| [images.association(:image)]}
end

(N.B. I have also tried the code commented out above was also tried) Most of the time when I run cucumber features, I get an error akin to the following:

No such file or directory - /tmp/stream,9887,0.png (Errno::ENOENT)

...

Sometimes the tests run successfully.

Can anyone tell me what the problem is I am having here or how they use FactoryGirl and Paperclip together to achieve something like what I am trying to achieve?

I am using Rails 3.

Community
  • 1
  • 1
Laz
  • 3,474
  • 9
  • 33
  • 46
  • 2
    Up vote on question just for using Keyzer Soze as you test name! – Breno Aug 01 '14 at 18:14
  • When I do this... photos get generated and placed in the public/system folder everytime I run the test suite. The photos don't get destroyed after the test suite is done running. Does anyone else notice this? – Jwan622 Jan 10 '16 at 19:13

8 Answers8

90

You can use fixture_file_upload

include ActionDispatch::TestProcess in your test helper, here is an example factory:

include ActionDispatch::TestProcess

FactoryBot.define do
  factory :user do
    avatar { fixture_file_upload(Rails.root.join('spec', 'photos', 'test.png'), 'image/png') }
  end
end

In the above example, spec/photos/test.png needs to exist in your application's root directory before running your tests.

Note, that FactoryBot is a new name for FactoryGirl.

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
DanS
  • 17,550
  • 9
  • 53
  • 47
  • 2
    This answer is totally right but it may be more clear to say that the TestProcess include needs to be included with the Factory and not simply in your spec_helper. – Andrew Hubbs Oct 23 '12 at 01:16
  • 2
    Thanks for the clarification, Andrew. You may also have to place "include ActionDispatch::TestProcess" before the FactoryGirl.define block (which isn't what happens in this gist: https://gist.github.com/313121). – Sam Dec 24 '12 at 15:44
  • 4
    Also, the `{ }` are required. It breaks without it – bigpotato Oct 18 '13 at 16:26
  • 3
    Further clarification: the `include ActionDispatch::TestProcess` should be inserted at the very top of the factory file, **outside** the `FactoryGirl.define do...end` block. – Frank Koehl Apr 23 '14 at 19:12
50

Newer FG syntax and no includes necessary

factory :user do
  avatar { File.new(Rails.root.join('app', 'assets', 'images', 'rails.png')) }
end

or, better yet,

factory :user do
  avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") } 
end
Neal
  • 4,468
  • 36
  • 33
  • 2
    I like it, but bear in mind that rails 4 removes app/assets/images/rails.png, so you will need to account for that change :) – stephenmurdoch Aug 06 '13 at 10:39
39

Desmond Bowe over at Pivotal Labs suggests avoiding fixture_file_upload due to memory leak problems. Instead, you should set the paperclip fields directly in your factory:

factory :attachment do
  supporting_documentation_file_name { 'test.pdf' }
  supporting_documentation_content_type { 'application/pdf' }
  supporting_documentation_file_size { 1024 }
  # ...
end
letz
  • 1,762
  • 1
  • 20
  • 40
Rick Quantz
  • 620
  • 5
  • 7
25

I've been using the code in the gist below:

Rails 2

http://gist.github.com/162881

Rails 3

https://gist.github.com/313121

deb
  • 12,326
  • 21
  • 67
  • 86
  • 1
    Just wondering the same things and this works beautifully. Thanks. I put it in an initializer. – Todd Nov 19 '10 at 22:29
  • 1
    Something to note: The gist above uses the older FactoryGirl syntax. DanS answer uses the new syntax. – Andrew Hubbs Oct 23 '12 at 01:17
14
file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Matthew Deiters
  • 151
  • 1
  • 2
4

Try using ActionController::TestUploadedFile. You can just set the file property to an instance of TestUploadedFile and paperclip should take care of the rest. For example

valid_file = File.new(File.join(Rails.root, 'features', 'support', 'file.png'))  
p.images { 
   [
     ActionController::TestUploadedFile.new(valid_file, Mime::Type.new('application/png'))
   ] 
}
britt
  • 325
  • 1
  • 6
0

The above answers in some cases can help, and the one actually helped in one of my situations, but when using a Carrierwave, the previous solution from this question didn't work out this time.

FIRST APPROACH:

For me adding an after :create solved the problem for me like this:

after :create do |b|
  b.update_column(:video_file, File.join(Rails.root, 'spec', 'fixtures', 'sample.mp4'))
end

Setting inline video file like video_file { File.new("#{Rails.root}/spec/fixtures/sample.mp4") } didn't work out and it was reporting errors.

SECOND APPROACH:

Define a factory like this (change personal_file to your attachment name):

FactoryGirl.define do
  factory :contact do
    personal_file { File.new("#{Rails.root}/spec/fixtures/personal_files/my_test_file.csv") }
    personal_file_content_type 'text/csv'

  end
end

And add these lines to theconfig/environemnts/test.rb :

config.paperclip_defaults = {
  url: "#{Rails.root}/spec/fixtures/:attachment/:filename",
  use_timestamp: false
}
Aleks
  • 4,866
  • 3
  • 38
  • 69
-1

What are you testing exactly? That paperclip will successfully attach the file? That really seems like a test that paperclip should handle, not your application.

Have you tried

a.data { File.join(Rails.root, 'features', 'support', 'file.png') }

We use Machinist instead of factory_girl and have just used things like

Image.blueprint do
  image { RAILS_ROOT + 'spec/fixtures/images/001.jpg' }
end

Though, we aren't really testing much when we do this, we typically just want to have a valid Image object.

theIV
  • 25,434
  • 5
  • 54
  • 58
  • I have models that require a valid association with a Paperclip-enabled model, itself with a valid attachment. – Eric Dec 29 '11 at 00:39