我在编写测试以确保字段:on_ride_photo
是布尔值时遇到问题。
我的验证电话:
validates :on_ride_photo,
inclusion: [true, false]
我的测试:
it { should validate_presence_of(:on_ride_photo) }
it { should ensure_inclusion_of(:on_ride_photo).in_array([true, false]) }
it { should_not allow_value(4).for(:on_ride_photo) }
it { should_not allow_value('lots').for(:on_ride_photo) }
工厂:
factory :coaster do
association :park
name 'Nemesis'
speed 60
height 60
length 160
inversions 4
on_ride_photo true
lat 52.98694
lng -1.88284
dates_ridden '24th Jul 2013:4, 9th Aug 2009:7'
powered false
end
错误:
失败:
1) 过山车验证不应该允许 on_ride_photo 设置为“lots”失败/错误:它 { should_not allow_value('lots').for(:on_ride_photo) } on_ride_photo 设置为“lots”时的预期错误,没有错误# ./spec/models/coaster_spec.rb:98:in `block (3 levels) in '
2) 过山车验证不应该允许 on_ride_photo 设置为 4 Coaster_spec.rb:97:in `block (3 levels) in '
3) 过山车验证不应允许将 power 设置为“yes”失败/错误:它 { should_not allow_value('yes').for(:powered) } 将 power 设置为“yes”时的预期错误,没有错误# ./spec/models/coaster_spec.rb:102:in `block (3 levels) in '
4) 过山车验证需要设置 on_ride_photo在列表中 (nil)"] # ./spec/models/coaster_spec.rb:95:in `block (3 levels) in '
5) Coaster 验证应确保包含 power in [true, false] 失败/错误:它 { should ensure_inclusion_of(:powered).in_array([true, false]) } [true, false] 与验证中的数组不匹配# ./spec/models/coaster_spec.rb:100:in `block (3 levels) in '
6) 过山车验证不应该允许将 power 设置为 4 Coaster_spec.rb:101:in `block (3 levels) in '
7) 过山车验证应确保在 [true, false] 失败/错误中包含 on_ride_photo:它 { should ensure_inclusion_of(:on_ride_photo).in_array([true, false]) } [true, false] 与验证中的数组不匹配# ./spec/models/coaster_spec.rb:96:in `block (3 levels) in '
:powered 也应该是一个布尔字段,因此我对其进行了相同的测试,因此这也是测试错误中出现的原因。