Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 + CarrierWave + Rspec: model validation

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?

like image 920
ExiRe Avatar asked Dec 01 '25 15:12

ExiRe


1 Answers

I solved it:

before(:each){ 
   @attr = { :description => "some text is here", 
             :image => File.open(File.join(Rails.root, '/spec/fixtures/files/violin.jpg')) } 
}
like image 68
ExiRe Avatar answered Dec 03 '25 08:12

ExiRe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!