在验收测试中加快回形针的图像处理速度

时间:2013-09-04 04:02:29

标签: ruby-on-rails performance rspec paperclip capybara

运行capybara功能规格时,我可以看到许多由factory_girl填充的慢速工厂通知。我认为,这些慢工厂的东西会严重减慢功能规格,甚至功能规格都是固有的慢速规格。然后我进行了一些检查,发现大部分慢工厂是由paperclip引起的。我们在这里使用了回形针模型:

FactoryGirl.define do
  factory :asset do
    image Rails.root.join('spec/fixtures/sample.jpg').open
  end
end

所以我想知道是否有像paperclip这样的测试模式加速测试。我在这里有一个简单的解决方案:只需复制原始文件而不是实际裁剪它。

2 个答案:

答案 0 :(得分:8)

您可以在工厂中设置回形针图像字段,这将导致回形针甚至无法处理图像:

factory :asset do        
  # Set the image fields manually to avoid uploading / processing the image
  image_file_name { 'test.jpg' }
  image_content_type { 'image/jpeg' }
  image_file_size { 256 }
end

答案 1 :(得分:4)

我找到了实现这一目标的方法,请参阅以下代码:

FactoryGirl.define do
  factory :asset do
    image_file_name { 'sample.jpg' }
    image_content_type 'image/jpeg'
    image_file_size 256

    after(:create) do |asset|
      image_file = Rails.root.join("spec/fixtures/#{asset.image_file_name}")

      # cp test image to direcotries
      [:original, :medium, :thumb].each do |size|
        dest_path = asset.image.path(size)
        `mkdir -p #{File.dirname(dest_path)}`
        `cp #{image_file} #{dest_path}`
      end
    end
  end
end

在创建挂钩后,将测试图像手动cp到factory_girl中的实际资产图像路径。它就像一个魅力。

相关问题