Rspec,Paperclip,Fabrication,有效对象而不保存到文件系统

时间:2012-10-01 17:06:50

标签: ruby-on-rails rspec paperclip ruby-on-rails-3.2

我有一个带有paperclip 3.2的rails 3.2应用程序,我有一个带有所需回形针附件(拇指)的模型。如何在不将文件保存到文件系统或S3的情况下创建有效对象。我目前拥有的是下面的内容,但每次运行时都会保存到文件系统中。有没有办法在没有每次上传的情况下拥有有效的剧集?

型号:

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

规格:

require 'spec_helper'

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

制造商:

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

1 个答案:

答案 0 :(得分:6)

发现这个:

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

Paperclip 3.0的

: Paperclip 3.0有一些重大变化,你现在应该使用这样的东西:

规格/支持/ 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