Carrierwave: Built-in RSpec matchers

CarrierWave comes with some RSpec matchers which will make testing more comfortable. Let's say you have an Uploader like this:

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  # Create different versions of your uploaded files:
  version :small do
    process resize_to_fill: [100, 100]
  end
  
  version :medium do
    process resize_to_fit: [200, nil]
  end
  
  version :large do
    process resize_to_limit: [400, 400]
  end

end

Imagine you have a class Movie with an attribute poster. In your spec file, you can add tests to check the dimensions of the several versions by writing

RSpec.describe Movie, type: :model do
  include CarrierWave::Test::Matchers

  describe '#poster' do
    before do
      @movie = create(:movie)
      @movie.poster.store!(File.open('spec/fixtures/test_poster.jpg'))
    end

    describe '.small' do
      it "scales down an image to be exactly 100 by 100 pixels" do
        expect(@movie.poster.small).to have_dimensions(100, 100)
      end
    end

    describe '.medium' do
      it "scales down an image to be no wider than 200 pixels" do
        expect(@movie.poster.medium).to be_no_wider_than(200)
      end
    end

    describe '.large' do
      it "scales down a landscape image to be no larger than 400 by 400 pixels" do
        expect(@movie.poster.large).to be_no_larger_than(400, 400)
      end
    end
  end
end

A more specific example can be found in the Readme Show archive.org snapshot of carrierwave. A list of all matchers can be found here Show archive.org snapshot .

Florian Leinsinger About 6 years ago