有载波上传字段的工厂 [英] Factory with carrierwave upload field

查看:60
本文介绍了有载波上传字段的工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要为模型建立Factory

Hello i need to build up Factory for my model, for example

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

目前,我只是将字符串#{n} .jpg传递到我的徽标字段中,这不起作用,如何评估此字段?我正在使用CarrierWave进行上传。

For now im just pass string "#{n}.jpg" into my logo field and this dont work, how to evalute this field? Im using CarrierWave for uploading.

推荐答案

您需要添加 ActionDispatch :: TestProcess 在您的工厂中(第3轨),并使用 fixture_file_upload 助手:

You need to include ActionDispatch::TestProcess in your factory (Rails 3), and use the fixture_file_upload helper:

include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :image do
    title "Example image"
    file { fixture_file_upload("files/example.jpg", "image/jpeg") }
  end
end

也请确保在 test / fixtures / files / example.jpg 中创建一个示例文件,否则您将得到一个错误消息,指出该文件不存在。

Also be sure to create an example file at test/fixtures/files/example.jpg, otherwise you will get an error that the file does not exist.

这篇关于有载波上传字段的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆