Gemfile · I codes and I blogs

Paperclip attachments in RSpec factories

Assuming one has a model whose attachments are handled by Paperclip, i.e.

$ rails generate paperclip user photo

app/models/user.rb:

class User < ActiveRecord::Base
  has_attached_file :photo
end

then to generate a test model with FactoryGirl, including the attachment:

spec/factories/user_factory.rb:

include ActionDispatch::TestProcess
FactoryGirl.define do
  factory :user do
    name 'Sample User'
    ...
    photo { fixture_file_upload(Rails.root.join('spec/fixtures/test_img.png'), 'image/png') }
  end
end

Various manipulations of the attached image through the web interface can then be tested with RSpec and Capybara.