Rspec, Paperclip, Fabrication, valid object without saving to filesystem

戏子无情 提交于 2019-12-12 09:47:19

问题


I have a rails 3.2 app with paperclip 3.2 and I have a model that has a required paperclip attachment(thumb). How can I create valid objects without having the file saved to the filesystem or S3. What I currently have is below but this saves to the filesystem on each run. Is there a way to have a valid episode without uploading everytime?

Model:

class Episode
  include Mongoid::Document
  include Mongoid::Paperclip
  has_mongoid_attached_file :thumb
  validates_attachment_presence :thumb
end

Spec:

require 'spec_helper'

describe Episode do
  it "has a valid factory" do
    Fabricate.build(:episode).should be_valid
  end
end

Fabricator:

Fabricator(:episode) do
  thumb { File.open(File.join(Rails.root, 'spec', 'fabricators', 'assets', 'thumb.jpg'))}
end

回答1:


Found this:

http://room118solutions.com/2011/05/25/stubbing-paperclip-during-testing/

for Paperclip 3.0: There were some significant changes in Paperclip 3.0, you should now use something like this:

spec/support/stub_paperclip_attachments.rb

module Paperclip
  class Attachment
    def save
      @queued_for_delete = []
      @queued_for_write = {}
      true
    end

  private
    def post_process
      true
    end
  end

  # This is only necessary if you're validating the content-type
  class ContentTypeDetector
  private
    def empty?
      false
    end
  end
end


来源:https://stackoverflow.com/questions/12677786/rspec-paperclip-fabrication-valid-object-without-saving-to-filesystem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!