stream7
stream7

Reputation: 1738

How to test carrierwave version size with Rspec

I have a CarrierWave::Uploader that produces three version of the uploaded image.

# Process files as they are uploaded:                                                                  
   process :resize_to_fit => [400, 400]                                                                   

   # Create different versions of your uploaded files:                                                    
   version :thumb do                                                                                      
     process :resize_to_fit => [60, 60]
   end

   version :small do
     process :resize_to_fit => [24, 24]
   end

And in my tests I try to verify the dimensions of the generated images

require 'spec_helper'
require 'carrierwave/test/matchers'

describe 'manufacturer logo uploader' do
  include CarrierWave::Test::Matchers

  before(:each) do
    image_path = Rails.root.join('test/fixtures/images', 'avatar100.gif').to_s
    @manufacturer = Factory.create(:manufacturer, :page_status => 1)
    @manufacturer.logo_image = File.open(image_path)
    @manufacturer.save!
  end

  context "manufacturer logo dimensions" do
    it "should have three versions" do
      @manufacturer.logo_image.should have_dimensions(400,400)
      @manufacturer.logo_image.thumb.should have_dimensions(60,60)
      @manufacturer.logo_image.small.should have_dimensions(24,24)
    end
  end

end

but this test depends on the actual image and resize_to_fit will not necessarily resize it to the specified dimensions. Any ideas on how to test this using stubs?

Upvotes: 4

Views: 2082

Answers (2)

equivalent8
equivalent8

Reputation: 14237

long shot but can you try to add this

before do
  DocumentUploader.enable_processing = true
end

because processing (current version and other versions) could be turned off by default for performance reason

had similar problem related to process set_file_name_to_model that was doing something setting "file_name" on model attribute

http://ruby-on-rails-eq8.blogspot.co.uk/2015/03/carrierwave-uploader-not-triggering.html

Upvotes: 0

BM5k
BM5k

Reputation: 1230

Here's my solution, which actually processes an image. This is slower than stubs, but verifies the actual resize (as long as the input image is larger than the target size).

describe 'images' do

  include CarrierWave::Test::Matchers

  before do
    MyUploader.enable_processing = true
  end

  it 'are resized' do
    path = Rails.root.join *%w[ spec data sample.png ]
    my_model = FactoryGirl.create :my_model, image: path.open

    my_model.artwork.small.should be_no_larger_than(300, 400)
  end

  after do
    MyUploader.enable_processing = false
  end

end

Upvotes: 2

Related Questions