我想为我的应用程序创建一些测试,但出现以下错误:
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
file
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
目的。我该如何解决这个错误?
Thanks.
我在寻找相同答案时遇到了你的问题。请检查一下:
如何使用Factory Girl生成回形针附件? https://stackoverflow.com/questions/3294824/how-do-i-use-factory-girl-to-generate-a-paperclip-attachment
祝你好运!
update:
以下是我将文件上传到工厂.rb 的步骤。
A.由于我使用rspec,所以我在spec/下创建了一个目录fixtures,在spec/fixtures/下创建了一个目录images,然后在其中放置了一个example.jpg图像,使得路径为Rails.root/spec/fixtures/images /示例.jpg
B.接下来,在我的factory.rb中,我更改了定义,如下所示:
Factory.define :image do |image|
image.image fixture_file_upload( Rails.root + 'spec/fixtures/images/example.jpg', "image/jpg")
image.caption "Some random caption"
end
(可选:如果在 rspec 中,请重新启动 spork 服务器)
C. 现在应该可以正常工作了。
如果您还有其他问题,请告诉我。我会尽力提供帮助:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)