Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip 4, Amazon S3 & rspec: how to stub file upload?

I'm writing tests with rspec and am a bit struggling with Paperclip 4. At the moment I'm using webmock to stub requests but image processing is slowing tests down.

Everything I read suggest to use stubs like so:

Profile.any_instance.stub(:save_attached_files).and_return(true)

It doesn't work since :save_attached_files disappeared with Paperclip 4 as far as I can tell.

What's the proper way to do it now ?

Thanks

like image 321
Arnaud Avatar asked Feb 18 '14 10:02

Arnaud


2 Answers

Add this to your rails_helper.rb in your config block:

RSpec.configure do |config|
  # Stub saving of files to S3
  config.before(:each) do
    allow_any_instance_of(Paperclip::Attachment).to receive(:save).and_return(true)
  end
end
like image 186
madcow Avatar answered Nov 08 '22 21:11

madcow


It doesn't exactly answer my question, but I found a way to run faster specs thanks to this dev blog, so I'm posting it if it can help someone else.

Just add this piece of code at the beginning of your spec file or in a helper. My tests are running 3x faster now.

# We stub some Paperclip methods - so it won't call shell slow commands
# This allows us to speedup paperclip tests 3-5x times.
module Paperclip
  def self.run cmd, params = "", expected_outcodes = 0
    cmd == 'convert' ? nil : super
  end
end
class Paperclip::Attachment
  def post_process
  end
end

Paperclip > 3.5.2

For newer versions of Paperclip, use the following:

module Paperclip
  def self.run cmd, arguments = "", interpolation_values = {}, local_options = {}
    cmd == 'convert' ? nil : super
  end
end

class Paperclip::Attachment
  def post_process
  end
end
like image 30
Arnaud Avatar answered Nov 08 '22 21:11

Arnaud