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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/1bbc5d1d891985f0d2391c709de83c60%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.