【问题标题】:Image attribute not saving in test with FactoryGirl使用 FactoryGirl 在测试中未保存图像属性
【发布时间】:2013-08-12 18:40:04
【问题描述】:

应用

我在使用载波上传器的表 User 中有一个列 bg_image

mount_uploader :bg_image, UserLogoUploader

它以如下所示的 activeadmin/formtastic 形式设置:

f.input :bg_image, :label => "Background Image", :as => :file

测试(问题出在哪里)

我有一个 User 的工厂来制作包含所有必需字段的基本版本(在其他测试中有效),但是当我尝试添加 bg_image 时它没有得到保存。

工厂:

FactoryGirl.define do
  factory :user do
    stuff "set in factory"
  end
end

测试文件:

let!(:user) { create(:user, :something => "that works", :bg_image => "doesntWork.png") }

it "inspects the element" do
  puts user.inspect
end

打印出来

#<User id: 1, stuff: "set in factory", something: "that works", bg_image: nil>

我似乎无法弄清楚为什么它不保存!

我尝试了一些方法,例如将其设置为 File 对象,但这不起作用,因为它需要是一个字符串

【问题讨论】:

    标签: ruby-on-rails activerecord activeadmin factory-bot rspec-rails


    【解决方案1】:

    我很确定它不起作用,因为在您的测试中您使用 bg_image 作为字符串,请记住您已将上传器安装到 bg_image 并且您需要使用它来让carrierwave 工作,我会查看carrierwave 文档以获取有关测试的信息,希望有帮助!

    这里有一些文档:

    https://github.com/carrierwaveuploader/carrierwave#testing-with-carrierwave

    【讨论】:

    • 该链接更多是测试上传,我想测试上传后使用的属性
    【解决方案2】:

    我的工厂镜像文件:

    包括 ActionDispatch::TestProcess FactoryGirl.define 做 工厂:图像做 文件 { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec','support', 'test_images', 'audi.png')) } 创建 Time.now.to_s 标签 { Faker::Lorem.characters 20 } 用户 { FactoryGirl.create(:user) } 结尾 结尾

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      相关资源
      最近更新 更多