2

我有照片模型,我通过 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

现在我想在我的模型规范中检查具有给定路径图像的模型将被保存(我将文件上传到给定路径中):

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.

什么是正确的处理方法?

4

1 回答 1

11

我解决了它:

before(:each){ 
   @attr = { :description => "some text is here", 
             :image => File.open(File.join(Rails.root, '/spec/fixtures/files/violin.jpg')) } 
}
于 2012-09-25T14:15:50.113 回答