Read more

Carrierwave: Built-in RSpec matchers

Florian Leinsinger
April 05, 2018Software engineer at makandra GmbH

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
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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 .

Posted by Florian Leinsinger to makandra dev (2018-04-05 09:38)