【问题标题】:Rspec, Paperclip, Fabrication, valid object without saving to filesystemRspec、Paperclip、Fabrication、有效对象而不保存到文件系统
【发布时间】:2012-09-22 13:38:36
【问题描述】:

我有一个带有回形针 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

【问题讨论】:

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


    【解决方案1】:

    找到这个:

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

    对于回形针 3.0: Paperclip 3.0 发生了一些重大变化,您现在应该使用以下内容:

    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
    

    【讨论】:

      猜你喜欢
      • 2020-02-13
      • 2012-05-11
      • 2011-08-25
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2016-07-03
      • 2014-10-08
      • 2017-12-14
      相关资源
      最近更新 更多