你好,我需要为我的模型建立工厂,例如
Factory.define :farm do |f|
f.name { Factory.next :name }
f.harvest '3'
f.offers 'Random'
f.latitude '43'
f.longitude '-70'
f.about 'We rocks!'
f.logo { Factory.next :logo } # this doesn't work
end
Run Code Online (Sandbox Code Playgroud)
现在我只是将字符串"#{n} .jpg"传递到我的徽标字段中,这不起作用,如何评估此字段?我正在使用CarrierWave进行上传.
有没有人知道使用factory_girl创建PaperClip 4.0附件的正确方法,绕过任何PaperClip处理和验证?
我以前只能在我的工厂做以下事情:
factory :attachment do
supporting_documentation_file_name { 'test.pdf' }
supporting_documentation_content_type { 'application/pdf' }
supporting_documentation_file_size { 1024 }
# ...
end
Run Code Online (Sandbox Code Playgroud)
这基本上会让PaperClip认为有一个有效的附件.
从3.5.3升级到4.0后,我现在收到验证错误:
ActiveRecord::RecordInvalid: Validation failed: Image translation missing: en.activerecord.errors.models.attachment.attributes.supporting_documentation.spoofed_media_type
Run Code Online (Sandbox Code Playgroud)
注意:PaperClip 3.X的原始讨论在这里:如何使用工厂女孩生成回形针附件?
我想为我的应用程序创建一些测试,我有以下错误:
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)>'
Run Code Online (Sandbox Code Playgroud)
这个错误是因为我的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
Run Code Online (Sandbox Code Playgroud)
测试期望一个Attachment
对象,但我正在创建一个Rack::Test::UploadedFile
对象.我该如何解决这个错误?
谢谢.