In my Factory I have a boolean field (evaluated), which I am trying to set to false. I want the field to be required, and always set to either true or false
Model
validates_presence_of :evaluated
Factory
FactoryGirl.define do
factory :submission do
evaluated true
score 1.5
ranking 1.5
submission_type "user"
.
.
end
end
In the test
it { should validate_presence_of(:evaluated) }
When I run it
Failure/Error: expect(@submission).to be_valid
expected #<Submission id: nil, competition_id: 163, user_id: 134, team_id: nil, evaluated: false, score: 1.5, ranking: 1.5, submission_type_cd: "user", withdrawn: true, withdrawn_date: "2016-02-08", created_at: nil, updated_at: nil> to be valid, but got errors: Evaluated can't be blank
If I change the value to true, the test passes
evaluated true
How can I set up a Factory with false values for Booleans?
I would rather use inclusion_of
validation instead of presence
validation.
From the rails doc,
If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use
validates_inclusion_of :field_name, in: [true, false]
This is due to the way Object#blank?
handles boolean values
i.e false.blank? # => true
Then your test would be something like,
it { should ensure_inclusion_of(:field_name).in_array([true, false]) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With