问题描述
我想为我的应用创建一些测试,但出现以下错误:
I want to create some tests for my app and I have the following error:
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
文件中有此错误
This error is because I have this on my 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
对象.我该如何解决这个错误?
The test expect an Attachment
object but I m creating an Rack::Test::UploadedFile
object. How can I solve this error?
谢谢.
推荐答案
我在寻找相同答案时遇到了您的问题.请检查以下内容:
I ran into your question while looking for the same answer. Please check this:
祝你好运!
更新:
这就是我逐步完成的将文件上传到我的factory.rb中的操作.
So here is what I did step by step to upload a file into my factories.rb.
A.由于我使用rspec,因此我在spec/下创建了目录装置,并在spec/fixtures/下创建了目录图像,然后在其中放置了example.jpg图像,因此路径为Rails.root/spec/fixtures/images/example .jpg
A. Since I use rspec, I created a directory fixtures under spec/ and a directory images under spec/fixtures/, and then put an example.jpg image in there, such that the path was Rails.root/spec/fixtures/images/example.jpg
B.接下来,在我的factory.rb文件中,我的定义如下:
B. Next, in my factories.rb, I changed my definition as followed:
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服务器)
(optional: restart your spork server if in rspec)
C.现在应该可以正常工作.
C. Should work fine now.
让我知道您是否还有其他问题.我会尽力帮助:)
Let me know if you have more issues. I'll do my best to help :)
这篇关于如何使用Carrierwave + FactoryGirl测试上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!