我有以下模型:
class Face < ActiveRecord::Base
attr_accessible :face_index, :design, :background
belongs_to :template
mount_uploader :background, BackgroundUploader
end
背景上传器:
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
在数据库中缺少上传器列之前,我已经看到过该错误,但是如果我的迁移对于开发人员来说是正确的,那么它们对于测试来说应该是正确的,不是吗?我是否需要规范中的某些内容才能使其工作?
谢谢!