Reputation: 4767
I have Photo model and i implemented file uploading via CarrierWave (my app works great):
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
Now i want to check in my model spec that model with given path image would be save (i uploaded the file in a given path):
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
But this doesn't work:
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.
What is a correct way to handle that?
Upvotes: 2
Views: 3750
Reputation: 4767
I solved it:
before(:each){
@attr = { :description => "some text is here",
:image => File.open(File.join(Rails.root, '/spec/fixtures/files/violin.jpg')) }
}
Upvotes: 11