开发者

Carrierwave Mongoid validation of file

I cant figure out how to validate that carrierwave has uploaded a document to my mongoid object.

i have a Document Class

class Content::Document < Content

  mount_uploader :attachment, DocumentUploader   

  field :attachable_id
  field :attachable_type
end

and an uploader:

require 'carrierwave/orm/mongoid'
class DocumentUploader < CarrierWave::Uploader::Base

  storage = :filesystem
  include CarrierWave::RMagick

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_开发者_如何学Goas}/#{model.id}"
  end          

  def extension_white_list
    %w(doc docx xls xlsx ppt pptx pdf )
  end

i would like to validate that the upload exists and that it matches the white list else through a standard validation error

this is on Rails 2.3.8


While it's true that Carrierwave does have extensive tests, you could test for validity with something like this:

it "is valid with valid attributes" do
    file_bytes = File.open("spec/binary/avatar.png")
    valid_attrs = {:name => "foo", :description => "bar", :avatar => file_bytes}
    user = User.new(valid_attrs)
    user.should be_valid
  end

Hope that helps!


In general you don't need to do that since this behaviour is already tested in carrierwave specs itself.

You can test your uploaders in isolation, using Carrierwave test helpers. E.g. I would just write a spec like

attachment_uploader.extension_white_list.should =~ %w(doc docx xls xlsx ppt pptx pdf)

But if you insist on testing that I would suggest using FakeFS to stub filesystem and then check with

File.exists? document.attachment.current_path

whether attachment has been created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜