我有照片模型,我通过 CarrierWave 实现了文件上传(我的应用程序运行良好):
class Photo < ActiveRecord::Base
attr_accessible :description, :image
mount_uploader :image, ImageUploader
validates :description, :length => { :maximum => 500, :message => :max_length_message }
validates :image, :presence => { :message => :presence_message }
end
现在我想检查我的模型规范是否会保存具有给定路径图像的模型(我在给定路径中上传了文件):
require 'spec_helper'
describe Photo do
before(:each){ @attr = { :description => "some text is here", :image => "#{Rails.root}/spec/fixtures/files/violin.jpg" } }
describe "DB" do
it "should create with valid params" do
expect do
Photo.create( @attr )
end.should change( Photo, :count ).by( 1 )
end
end
end
但这行不通:
1) Photo DB should create with valid params
Failure/Error: Photo.create( @attr )
CarrierWave::FormNotMultipart:
You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.
If this is a file upload, please check that your upload form is multipart encoded.
什么是正确的处理方式?
最佳答案
我解决了:
before(:each){
@attr = { :description => "some text is here",
:image => File.open(File.join(Rails.root, '/spec/fixtures/files/violin.jpg')) }
}
关于ruby-on-rails - Rails 3 + CarrierWave + Rspec : model validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584111/