2011-09-25 79 views
3

我的应用程序做了很多与图像。我用回形针将它们附加到模型上。我有大量的测试(测试::单元),涉及到创建图像,这些运行非常缓慢。如何加快涉及图片上传/调整大小的Rails单元测试?

我使用FactoryGirl在我的测试中创建模型。这是我如何创建图像附件:

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

我该如何伪造图像上传或以其他方式加快速度?

回答

1

This snippet工作对我来说:

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

    # tests... 
end 

UPD。我现在的选择是在全球范围内存根出ImageMagic,通过添加以下到我的test_helper.rb中:

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

(从here改编 - 顺便说一句,你可能想,如果你有兴趣看看这篇文章在加快你的测试)