I am using Carrierwave, carrierwave_backgrounder
, Sidekiq and RSpec.
Without the background jobs I tested the processing of my Carrierwave uploader by doing:
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
These test does mostly repeat the Uploader implementation but could contain actual logic which needs testing.
I cannot get the uploader to process in the test.
On the carrierwave_backgrounder
page they say testing with RSpec has some issues due to after_commit
hooks not getting called. I tried to use the TestAfterCommit
-gem with no luck.
Uploader:
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
Model:
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