Carrierwave上传适用于rails控制台,但不符合规范

时间:2012-05-16 01:39:47

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

我有以下型号:

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

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

谢谢!

1 个答案:

答案 0 :(得分:5)

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

rake db:test:load

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