Posted 8 months ago. Visible to the public. Repeats. Linked content.

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 = FactoryBot.create(:movie)!('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 of carrierwave. A list of all matchers can be found here.

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Florian Leinsinger
Last edit:
8 months ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Florian Leinsinger to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more