简体   繁体   English

如何加快涉及图像上传/调整大小的Rails单元测试?

[英]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. 我有很多涉及创建图像的测试(Test :: Unit),这些测试运行起来非常慢。

I use FactoryGirl to create models in my tests. 我使用FactoryGirl在我的测试中创建模型。 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. UPD。 My current preference is to stub out ImageMagic globally, by adding the following to my test_helper.rb: 我目前的偏好是通过在test_helper.rb中添加以下内容来全局存取ImageMagic:

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) (改编自这里 - 顺便说一句,如果你对加速测试感兴趣,你可能想看看这篇文章)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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