Read more

Custom RSpec matcher for allowed values (or assignable_values)

Dominik Schöler
September 04, 2013Software engineer at makandra GmbH

In contrast to RSpec's included allow_value matcher, the attached matcher will also work on associations, which makes it ideal for testing assignable_values.

Usage example

describe Unit do

  describe '#building' do
    it 'should only allow buildings that a user has access to' do
      building = build(:building)
      other_building = build(:building)
      unauthorized_building = build(:building)

      power = Power.new(build(:user))

      Power.with_power(power) do
        expect(power).to receive(:buildings).at_least(1).time.and_return [building, other_building]
        expect(build(:unit)).to allow_association_values(building, other_building).for(:building)
        expect(build(:unit)).not_to allow_association_values(unauthorized_building, nil).for(:building)
      end
    end
  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

Alternatively you could test explicitly for the method that assignable_values generates:

...
      Power.with_power(power) do
        expect(power).to receive(:buildings).at_least(1).time.and_return [building, other_building]
        
        unit = build(:unit)
        expect(unit.assignable_buildings).to contain_exactly(building, other_building)
        expect(unit.assignable_buildings).not_to include(unauthorized_building, nil)
      end
...

See RSpec: Where to put custom matchers and other support code

Posted by Dominik Schöler to makandra dev (2013-09-04 11:26)