简体   繁体   中英

How can I speed up Rails unit tests involving image upload/resizing?

My app does a lot with images. I use paperclip to attach them to models. I have tons of tests (Test::Unit) that involve creating images, these run pretty slowly.

I use FactoryGirl to create models in my tests. This is how I create image attachments:

factory :product_image_100_100 do
    image File.new(File.join(::Rails.root.to_s, "/test/fixtures/images", "100_100.jpg"))
end

How can I fake the image upload or otherwise speed things up?

This snippet worked for me:

require 'test_helper'
class PhotoTest < ActiveSupport::TestCase
  setup do
    Paperclip::Attachment.any_instance.stubs(:post_process).returns(true)
  end

  # tests...
end

Upd. My current preference is to stub out ImageMagic globally, by adding the following to my test_helper.rb:

module Paperclip
 def self.run(cmd, *)
   case cmd
   when "identify"
     return "100x100"
   when "convert"
     return
   else
     super
   end
 end
end

(Adapted from here – btw, you may want to take a look at this article if you're interested in speeding up your tests)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM