Stubbing Paperclip Attachments in Rspec

If you are using RSpec, paperclip, and your models are validating the existence of attachments, you are going to want to stub out the saving of the attachments. You will want to do this to speed up your test, keep from having extraneous files that you need to clean up (those saved by running your test).

Adding the code below to my RSpec suppot folder, sped up my testing suite from 1~2 minutes to about 10-15sec.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

Comments