Skip to content

Instantly share code, notes, and snippets.

@joshuaballoch
Created October 23, 2012 09:36
Show Gist options
  • Save joshuaballoch/3937901 to your computer and use it in GitHub Desktop.
Save joshuaballoch/3937901 to your computer and use it in GitHub Desktop.
rack test error running paperclip rspec and accepts_nested_attributes_for
#Association Mismatch occurs when trying to create a project using Factory Girl:
Failures:
1) Project#address_city should works
Failure/Error: create(:project).address_city.should be_nil
ActiveRecord::AssociationTypeMismatch:
ProjectImage(#70115373883740) expected, got Rack::Test::UploadedFile(#70115311431900)
# ./spec/models/project_spec.rb:194:in `block (3 levels) in <top (required)>'
Finished in 2.66 seconds
1 example, 1 failure
#What I changed the factory to:
FactoryGirl.define do
factory :project do |f|
f.status "Completed"
f.title {Randgen.word}
f.brand {FactoryGirl.create :designer}
f.body {Randgen.sentence}
f.user {FactoryGirl.create(:user)}
f.images [Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/files/steelbrickbamboo.jpg", "image/jpeg")]
f.privacy ProjectPrivacy::PUBLIC
end
end
#Why: Projects accept nested attributes for images now (excerpt from project.rb):
has_many :images, :class_name => "ProjectImage", :as => :attachable
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
#Former Factory:
FactoryGirl.define do
factory :project do |f|
f.status "Completed"
f.title {Randgen.word}
f.brand {FactoryGirl.create :designer}
f.body {Randgen.sentence}
f.user {FactoryGirl.create(:user)}
f.project_image { Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/files/steelbrickbamboo.jpg", "image/jpeg")}
f.privacy ProjectPrivacy::PUBLIC
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment