简体   繁体   English

Rails 4使用夹具(.yml)和回形针测试上传文件

[英]Rails 4 test upload file using fixtures (.yml) and paperclip

How can i create fixtures file for test paperclip upload? 如何创建用于测试回形针上传的灯具文件? I search fews result in google but always use with FactoryGirl. 我在Google搜索少数结果,但始终与FactoryGirl一起使用。 I tried but not work: 我试过但没有用:

img:
  image: <%= fixture_file_upload(Rails.root.join('test/fixtures/test_img.jpg'), 'image/jpeg') %>

You cannot do that with fixtures. 您无法使用灯具做到这一点。 Fixtures are adding attribute values directly to the database, skipping the active record layer. 固定装置将属性值直接添加到数据库中,从而跳过了活动记录层。 Paperclip is a plugin for ActiveRecord, that processing and storing photos. Paperclip是ActiveRecord的插件,用于处理和存储照片。

Saving the real image can be done only passing arguments directly to the new record. 仅将参数直接传递给新记录才能完成保存真实图像的操作。

What you can do is to put attributes into the fixture so the paperclip will work as it should, just without real file. 您可以做的是将属性放入灯具,以便回形针可以正常工作,而无需真实文件。

  photo_file_name: temp_file.jpg
  photo_content_type: image/jpeg
  photo_file_size: 223312
  photo_updated_at: 2015-02-29 10:30:19 Z

When you want to make everything correct, better to use FactoryGirl . 当您想使所有事情正确时,最好使用FactoryGirl

fixture_file_upload is only used for action controller. fixture_file_upload仅用于动作控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM