有没有人知道使用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正在尝试查找您上传的“文件”的mime类型.如果没有,则验证失败,以保护您免受文件类型欺骗.
我自己没试过,但也许你最好的选择是使用一个真实的文件,并使用ActionDispatch :: TestProcess中的fixture_file_upload方法设置它.
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
您需要在test_helper.rb中包含ActionDispatch :: TestProcess
这是第一次发布here.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。