Posted 16 days ago. Visible to the public. Repeats.

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:

Copy
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

Copy
RSpec.describe Movie, type: :model do include CarrierWave::Test::Matchers describe '#poster' do before do @movie = FactoryBot.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 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.

Author of this card:

Avatar
Florian Leinsinger
Last edit:
15 days 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