Rails検証の包含エラー「リストに含まれていません」
概要
MySQL データベースにこのモデルのテーブルがあります。
class Client< ActiveRecord::Base
validates :name, :length => {:maximum => 255}, :presence => true
validates :client_status, inclusion: { in: 0..2, :presence => true }
validates :client_type, inclusion: { in: 0..2, :presence => true}
end
したがって、 client_status と client_type を 0 から 2 までの数値のみにしたいのですが、これに対応するために私が書いた rspec は次のとおりです。
describe Client do
before do
@client = Client.new
end
it "should allow name that is less than 255 characters" do
long_char = 'a' *254
@client.name = long_char
@client.client_status = 0
@client.client_type = 1
@client.should be_valid
end
end
これは非常に単純なテストです。 client_status と client_type の両方に true があるため、それらを RSPEC に追加する必要があります。ただし、この rspec を実行すると、次のエラー メッセージが表示されます。
got errors: Value type is not included in the list, Status is not included in the list
出力が何であるかを確認するためにこれを試しました:
puts "client type is: #{@client.client_type} and status is: #{@client.client_status} ."
次の出力を受け取りました。
client type is: false and status is: .
注: 会社の NDA に違反しないように、モデル/rspec の名前と一部のフィールドを変更しました。
解決策
validates :client_status, presence: true, inclusion: { in: 0..2 }
validates :client_status, inclusion: { in: 0..2 }