Here’s a quick little trick that I used to speed up my tests involving Paperclip by about 70%.
I posted it over on the Paperclip Google Group, which is a friendly and active place to hang out if you’re a Paperclip user.
Here’s an example using Test::Unit, which is still my favorite way to test🙂
require 'test_helper' class PhotoTest < ActiveSupport::TestCase setup do Photo.any_instance.stubs(:save_attached_files).returns(true) Photo.any_instance.stubs(:delete_attached_files).returns(true) Paperclip::Attachment.any_instance.stubs(:post_process).returns(true) end # tests... end
The really important bit is stubbing out the post_process method. That took my unit tests down from 51.77 to 15.14 seconds. That’s a HUGE win, especially if you consider slow tests to be a bug.
I’m not sure what kind of impact this has on test coverage, so you may want to consider not stubbing out the Paperclip internals in every case. I’ve got some separate “remote” tests that I run before deployments that make me feel warm and fuzzy enough. Let me know what you think about it. I’ve had really good results so far!