有人知道使用PaperClip创建factory_girl 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的最初讨论如下:如何使用“工厂女孩”生成剪纸夹附件?
发布于 2014-05-01 22:32:42
这个问题似乎是由检波器引起的。
回形针试图找到你上传的“文件”的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中。
这是第一次发布这里。
https://stackoverflow.com/questions/21539189
复制相似问题