用于测试Carrierwave上传器的RSpec和FactoryGirl设置

时间:2016-11-30 00:13:54

标签: ruby-on-rails ruby rspec carrierwave factory-bot

我正在尝试设置RSpec和FactoryGirl来测试我的载波上传器

上传器型号代码

# app\models\go_upload.rb
class GoUpload < ActiveRecord::Base
  mount_uploader :attachment, GoAttachmentUploader
  validates :name, :subname, :person_name, :attachment, presence: true
end

工厂代码,指的是https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Use-test-factories

factory :go_upload do
  name "foo"
  subname "bar"
  person_name "testimage"
  attachment  {Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/fixtures/testfiles/testimage.jpg')))}
end

GoAttachmentUploader代码

#app\uploaders\go_attachment_uploader.rb
class GoAttachmentUploader < CarrierWave::Uploader::Base

 storage :file

 def store_dir(file="go_contact/#{model.subname}")
  file
 end

 def extension_white_list
  %w(jpg jpeg gif png)
 end

 def filename
  "#{model.person_name}.jpg"
 end
end

问题在于,当我使用工厂女孩创建一个go_upload对象时,所有属性(&#34; name&#34;,&#34; subname&#34;&#34; person_name&#34;)对象获得期望值&#34;附件&#34;是没有了#39; 我想&#34;附件&#34;的格式在数据库中是String,和 {Rack :: Test :: UploadedFile.new(File.open(File.join(Rails.root,&#39; /spec/fixtures/testfiles/testimage.jpg')))}给出哈希,所以我分配一个字符串到&#34;附件&#34;,但也失败了,仍然得到&#34;附件&#34;为零。

代码在开发环境中运行良好,但我正在尝试编写测试,所以任何人都可以帮助我解决这个问题,非常感谢。

0 个答案:

没有答案