加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Carrierwave上传工作在rails控制台但不符合规

发布时间:2020-12-17 03:14:21 所属栏目:百科 来源:网络整理
导读:我有以下型号: class Face ActiveRecord::Base attr_accessible :face_index,:design,:background belongs_to :template mount_uploader :background,BackgroundUploaderend BackgroundUploader: class BackgroundUploader CarrierWave::Uploader::Base de
我有以下型号:

class Face < ActiveRecord::Base
  attr_accessible :face_index,:design,:background
  belongs_to :template
  mount_uploader :background,BackgroundUploader
end

BackgroundUploader:

class BackgroundUploader < CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

当我启动rails控制台时,我可以创建一个Face并为其保存背景:

f = Face.create(:face_index => 0)
f.background = File.open("/path/to/image.jpg")
f.save!

这一切都有效,但当我尝试将其移至rspec时,我遇到了失败:

Failures:

  1) Face A new face 
     Failure/Error: @face.background = File.open(image_path)
     NoMethodError:
       undefined method `background_will_change!' for #<Face:0x007ff63d9f7410>

规格:

describe Face do

  before(:each) do
    image_path = Rails.root.join('spec/support/images','02.jpg').to_s
    @face = FactoryGirl.create(:face)
    @face.background = File.open(image_path)
    @face.save!
  end

  describe "A new face" do
    it { should belong_to(:template) }
  end

end

工厂:

FactoryGirl.define do    
  factory :face do
    face_index 0
  end
end

我在db上缺少uploader列之前就已经看到了这个错误,但如果我的迁移对于dev是正确的,那么它们应该是正确的测试,非?我是否需要在规范中要求使其有效?

谢谢!

解决方法

Doh,答案是我的最后一段,我忘了跑

rake db:test:load

休息并回到问题的一个很好的例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读