如何测试:RSpec + Carrierwave + Carrierwave_backgrounder + Sidekiq

时间:2016-10-03 07:18:53

标签: ruby-on-rails rspec carrierwave

我正在使用Carrierwave,carrierwave_backgrounder,Sidekiq和RSpec。

在没有后台工作的情况下,我通过以下方式测试了Carrierwave上传器的处理:

before do
  ImageUploader.enable_processing = true
  File.open('spec/assets/image.jpg') do |f|
    uploader.store!(f)
  end
end

after do
  ImageUploader.enable_processing = false
  uploader.remove!
end

context 'the thumb version' do
  it 'should scale down a landscape image to be exactly 350 by 350 pixels' do
      uploader.thumb.should have_dimensions(350, 350)
    end
  end
end

context 'the view version' do
  it 'should scale down a landscape image to fit within 1680 by 10000 pixels' do
    uploader.large.should be_no_larger_than(1680, 10000)
  end
end

这些测试主要重复上传器实现,但可能包含需要测试的实际逻辑。

我无法让上传者在测试中处理。 在carrierwave_backgrounder页面上,他们说使用RSpec进行测试存在一些问题,因为after_commit挂钩未被调用。我试图使用TestAfterCommit - gem没有运气。

者:

class ImageUploader < BaseImageUploader
  include ::CarrierWave::Backgrounder::Delay
  before :cache, :save_original_filename

  storage :file

  # Resizes to width 1680px (if the image is larger)
  version :large do
    process resize_to_fit: [1680, 10000]
    process :watermark

    def store_dir
      %(uploads/#{model.parent.class.name.pluralize.downcase}/#{model.parent.id}/large)
    end
  end

  # Creates a thumbnail version
  version :thumb do
    process resize_to_fill: [350, 350]

    def store_dir
      %(uploads/#{model.parent.class.name.pluralize.downcase}/#{model.parent.id}/thumb)
    end
  end

型号:

class Image < ActiveRecord::Base   
  mount_uploader :file, ImageUploader
  process_in_background :file

  def original
    file.url
  end

  def thumb
    file.thumb.url
  end

  def view
    file.large.url
  end
 end

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,已解决,包括我的import app.helper文件:

rails_helper.rb