I'm using Rspec and FactoryGirl to create specs for multimodel paperclip attachments. From searching other questions, it seems like a single image attachment is more common.
Car.rb
class Car < ActiveRecord::Base has_many :uploads, dependent: :destroy
validates :uploads, presence: { message: 'You must upload at least one image' }
accepts_nested_attributes_for :uploads, allow_destroy: true end Upload.rb
class Upload < ActiveRecord::Base belongs_to :car
has_attached_file :upload, styles: { medium: '300x300>', thumb: '100x100>' }
validates_attachment_content_type :upload, content_type: /\Aimage\/.*\Z/ validates_attachment_presence :upload end
As you can see there is a has_many and belongs_to relationship between the two.
car_spec.rb
require 'spec_helper'
describe Car do before do @car = Car.new(FactoryGirl.attributes_for(:car)) end
subject { @car }
it { should respond_to(:uploads) }
it { should be_valid } end
cars.rb factory
FactoryGirl.define do factory :car do # Something uploads end end I have tried `uploads { FactoryGirl.create!(:upload) }` as well as using the `fixture_file_upload` directly on the Car's uploads attribute. Also I read about Associations,but I wasn't sure how that work.
For the upload factory I've done how you would test an individual attachment
Uploads.rb
include ActionDispatch::TestProcess
FactoryGirl.define do factory :upload do upload { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'support', 'test.jpg'), 'image/jpeg') } end end