有谁知道使用 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
这基本上会诱使 PaperClip 认为存在有效的附件。
从 3.5.3 升级到 4.0 后,我现在收到验证错误:
ActiveRecord::RecordInvalid: Validation failed: Image translation missing: en.activerecord.errors.models.attachment.attributes.supporting_documentation.spoofed_media_type
注意:PaperClip 3.X 的原始讨论在这里:How Do I Use Factory Girl To Generate A Paperclip Attachment?
最佳答案
该问题似乎是由 line 61 in media_type_spoof_detector 引起的.
Paperclip 正在尝试查找您上传的"file"的 MIME 类型。如果没有,则无法通过验证来保护您免受文件类型欺骗。
我自己还没有尝试过,但也许最好的办法是使用真实文件,并使用 fixture_file_upload
设置它。方法来自 ActionDispatch::TestProcess
.
factory :attachment do
supporting_documentation { fixture_file_upload 'test.pdf', 'application/pdf' }
# This is to prevent Errno::EMFILE: Too many open files
after_create do |attachment, proxy|
proxy.supporting_documentation.close
end
end
您需要包含
ActionDispatch::TestProcess
在 test_helper.rb
这是第一次发布 here .
关于ruby-on-rails - 将 factory_girl 与 PaperClip 4.0 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539189/