Error while checking enumeration value using match tags

In my device model, I have

 enum device_type: { ios: 1 , android: 2 } validates :device_type, presence: true, inclusion: { in: device_types.keys } 

And in my device_spec.rb , I am writing several tests for this, for example

 describe 'validations' do subject { FactoryGirl.build(:device) } it { is_expected.to allow_values('ios', 'android').for(:device_type) } it { is_expected.to validate_inclusion_of(:device_type).in_array(%w(ios android)) } it { is_expected.not_to allow_value('windows').for(:device_type) } end 

When I ran rspec, the allow_values ​​test allow_values('ios', 'android') was passed, but the other two were unsuccessful.

1) The device must ensure the inclusion of device_type in ["ios", "android"]

Error / Error: this is {is_expected.to validate_inclusion_of (: device_type) .in_array (% w (ios android))}

  ArgumentError: '123456789' is not a valid device_type 

2) The device should not allow the installation of the device_type parameter in "windows"

Error / Error: this is {is_expected.not_to allow_value ('windows'). for (: device_type)}

  ArgumentError: 'windows' is not a valid device_type 

"This is an invalid device_type type," but why did these tests fail?

+8
source share
1 answer

When you define an attribute as enum, you can just test it with matches

 it { should define_enum_for(:device_type).with(:ios, :android) } 

If you try to assign any other value, ActiveRecord will raise an ArgumentError (not a valid device type)

0
source

Source: https://habr.com/ru/post/1239357/


All Articles