2011-09-25 33 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改編 - 順便說一句,你可能想,如果你有興趣看看這篇文章在加快你的測試)