Created
May 30, 2010 13:26
-
-
Save jeffrafter/419014 to your computer and use it in GitHub Desktop.
factory_girl attachments for paperclip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'action_controller/test_process' | |
# Paperclip attachments in factories, made easy based on technicalpickles | |
Factory.class_eval do | |
def attach(name, path, content_type = nil) | |
if content_type | |
add_attribute name, ActionController::TestUploadedFile.new("#{RAILS_ROOT}/#{path}", content_type) | |
else | |
add_attribute name, ActionController::TestUploadedFile.new("#{RAILS_ROOT}/#{path}") | |
end | |
end | |
end | |
Factory.sequence :badge do |n| | |
"name#{n}" | |
end | |
Factory.define :badge do |badge| | |
badge.name { Factory.next :badge } | |
badge.active { true } | |
badge.description { "You get this when you are awesome" } | |
badge.tags { "cool awesome" } | |
badge.site { |badge| badge.association(:site) } | |
badge.attach ( "image", "test/fixtures/flower.png", "image/png" ) | |
end |
You can simplify further by just adding "include ActionDispatch::TestProcess" to test_helper.rb, and then just using the following in your factory
f.field_name { fixture_file_upload 'filename', 'mimetype' }
I realize this is a pretty old post, but wasn't sure if anyone here has run into an issue when using S3 for storage. I'm using the code mentioned by sr3d, but am getting AWS::S3::RequestTimeTooSkewed
in my test results when attempting to use a factory that contains an "upload" file.
Dont work for Rails 4.1
.rvm/gems/ruby-2.1.0@banners/gems/factory_girl-4.4.0/lib/factory_girl/definition_proxy.rb:42:in `add_attribute': wrong number of arguments (4 for 1..2) (ArgumentError)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Rails 3, your gist actually doesn't work. After looking at the Rails code, this code works:
My model is
The CSV file is actually under test/fixtures/files/ and since they are not real fixture data, but actual CSV data for testing the data import process, I had to rename them as .csv.upload otherwise Rails's test will try to populate the test databases with them (and fail miserably)
I'm using Rails 3b4, with latest paperclip 61f74...eed69-master