我对模型的属性进行了以下验证:
validates :on_ride_photo,
presence: true,
inclusion: { in: [true, false] }
然后我有以下测试:
context 'on_ride_photo' do
it { should validate_presence_of(:on_ride_photo) }
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) }
it { should_not allow_value(4).for(:on_ride_photo) }
it { should_not allow_value('yes').for(:on_ride_photo) }
end
但是在运行我的规范时出现以下错误:
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 <top (required)>'
我想允许 false 作为有效值的事实是否被它必须存在并且 false 被归类为不存在的事实所推翻?如果是这样,那么我该如何解决这个问题?