【发布时间】:2012-03-28 05:43:36
【问题描述】:
我想为我的应用创建一些测试,但出现以下错误:
1) User feeds ordering should order feeds by id desc
Failure/Error: @post_1 = FactoryGirl.create(:post)
ActiveRecord::AssociationTypeMismatch:
Attachment(#87413420) expected, got Rack::Test::UploadedFile(#81956820)
# ./spec/models/user_spec.rb:37:in `block (3 levels) in <top (required)>'
此错误是因为我的 factories.rb 文件中有此错误
factory :post do
title "Lorem Ipsum"
description "Some random text goes here"
price "500000"
model "S 403"
makes "Toyota"
prefecture "Aichi-ken"
contact_info "ryu ryusaki"
year "2012"
shaken_validation "dec/2014"
attachments [ Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/example.jpg"), "image/jpeg") ]
#attachments [ File.open(Rails.root.join("spec/fixtures/files/example.jpg")) ]
end
测试需要一个Attachment 对象,但我正在创建一个Rack::Test::UploadedFile 对象。我该如何解决这个错误?
谢谢。
【问题讨论】:
标签: unit-testing rspec2 factory-bot