awesomeprogrammer.com

Sharing ideas

Solving problems

Gathering solutions

Exchanging thoughts

Ruby On Rails

PHP

Postgres

Debian & Ubuntu
jQuery & CSS

Stubbing Paperclip File Upload With Rspec

Here’s small chunk of code that will help you out with testing paperclip. Basically it will stub any fie upload of any instance for given model. So now you won’t be left with ridiculous amount of files created after each test run. Yay!

1
2
3
4
5
6
7
8
9
10
11
module PaperclipMacros

  def stub_paperclip(model)
    before do
      model.any_instance.stub(:save_attached_files).and_return(true)
      model.any_instance.stub(:delete_attached_files).and_return(true)
      Paperclip::Attachment.any_instance.stub(:post_process).and_return(true)
    end
  end

end

Put that file somehere under spec/support/ and in your spec_helper.rb include it like using config.extend PaperclipMacros. Now later in your test (of models or controllers, whatever) you can call it like that:

1
2
3
4
describe Download do
  stub_paperclip(Download)
  ...
end

Comments