Exi*_*iRe 2 rspec ruby-on-rails ruby-on-rails-3 carrierwave
我有Photo模型,我通过CarrierWave实现文件上传(我的应用程序很棒):
class Photo < ActiveRecord::Base
attr_accessible :description, :image
mount_uploader :image, ImageUploader
validates :description, :length => { :maximum => 500, :message => :max_lenght_message }
validates :image, :presence => { :message => :presence_message }
end
Run Code Online (Sandbox Code Playgroud)
现在我想检查我的模型规范,给定路径图像的模型将被保存(我在给定路径中上传文件):
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
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
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.
Run Code Online (Sandbox Code Playgroud)
处理这个问题的正确方法是什么?
Exi*_*iRe 11
我解决了它:
before(:each){
@attr = { :description => "some text is here",
:image => File.open(File.join(Rails.root, '/spec/fixtures/files/violin.jpg')) }
}
Run Code Online (Sandbox Code Playgroud)