Sunday, 8 September 2013

rSpec Shoulda matchers testing validations

rSpec Shoulda matchers testing validations

I have the following validation on an attribute of a model:
validates :on_ride_photo,
presence: true,
inclusion: { in: [true, false] }
I then have the following tests:
it { should allow_value(true).for(:on_ride_photo) }
it { should allow_value(false).for(:on_ride_photo) }
it { should_not allow_value(nil).for(:on_ride_photo) }
But I get the following error when running my specs:
2) Coaster validations should allow on_ride_photo to be set to false
Failure/Error: it { should allow_value(false).for(:on_ride_photo) } Did
not expect errors when on_ride_photo is set to false, got error: #
./spec/models/coaster_spec.rb:86:in `block (3 levels) in '
Is the fact that I want to allow false as a valid value being knocked down
by the fact it has to be present and that false is classes as not present?
If so, then how can I work around this?

No comments:

Post a Comment