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

4

11

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

Excrescency answered 18/2, 2014 at 10:12 Comment(0)
B
16

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
Beside answered 10/9, 2015 at 22:1 Comment(4)
Interesting. From my understanding though, this is the analog of the old stub way and I assumed the proper way to do it. Will try the alternatives out!Beside
yeah I was a little disappointed, it looked like an interesting/clean solution :)Excrescency
This made my tests faster, not slower. Using this stub and the Paperclip class overrides in @Nycen's answer really increased the speed of my tests that involved Paperclip attachments. Cheers.Delanos
This made my tests faster too.Vickery
E
4

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
Excrescency answered 19/2, 2014 at 14:44 Comment(4)
Since you've said this answer doesn't answer your question, shouldn't @madcow's answer be the accepted answer since it does address your question?Mineralogy
Awesome, got a downvote 3 years later because I didn't accept an answer that was posted 1.5 years after I answered my own question. I would've been better not answering at all. Good lesson here.Excrescency
I didn't downvote you, and it doesn't seem like the downvoter provided a reason why they did. You may be jumping to conclusions.Mineralogy
I jumped to that conclusion too ;) It doesn't mean I'm also right about the downvote though. Anyway, that was a stupid comment in a moment of weakness.Excrescency
C
1

I had a heckuva time dealing with this. I didn't want to test Paperclip per se -- rather, I needed a file to exist on my model so I can test a very sensitive custom class.

None of the other answers worked for me (Rails 5, Rspec 3.5) so I gave up on stubbing AWS or using the Paperclip::Shoulda::Matchers. Nothing worked and it ate up an entire day! Finally I gave up on Paperclip altogether and went this route:

list = build(:list)
allow(list).to receive_message_chain("file.url") { "#{Rails.root}/spec/fixtures/guess_fullname.csv" }
importer = Importer.new(list)
expect(importer.set_key_mapping).to eq({nil=>:email_address, :company=>:company, :fullname=>:full_name})

Where my models/list.rb has has_attached_file :file and Importer is a class I wrote that takes the file, which was already uploaded to S3 by paperclip before the class is initialized, and processes it. The file.url would otherwise be the S3 URL, so I give it the path in my repo to a testing csv file. set_key_mapping is a method in my class that I can use to verify the processing part worked.

Hope this saves somebody a few hours...

Cement answered 27/6, 2017 at 2:27 Comment(0)
E
0

What about adding AWS.stub! in your spec/spec_helper.rb? Give that a try.

Encode answered 28/5, 2014 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.