How do I write test for presence and validity of boolean values?
db/migrate/[timestamp]_add_admin_to_users.rb
add_column :users, :is_female, :boolean, default: false
app/models/user.rb
validates_inclusion_of :is_female, in: [true, false]
test/models/user_test.rb
test "gender should be valid" do
@user.is_female = "0"
assert @user.valid?
@user.is_female = "1"
assert @user.valid?
@user.is_female = "f"
assert @user.valid?
@user.is_female = "t"
assert @user.valid?
@user.is_female = "false"
assert @user.valid?
@user.is_female = "true"
assert @user.valid?
end
test "gender should be present" do
@user.is_female = " "
assert_not @user.valid?
end
I get the following error.
suai@tutorial:~/workspace/converse (gender-birthday) $ rake test
Run options: --seed 54502
# Running:
......................F.........................................
Finished in 5.003949s, 12.7899 runs/s, 59.9527 assertions/s.
1) Failure:
UserTest#test_gender_should_be_present [/home/ubuntu/workspace/converse/test/models/user_test.rb:26]:
Expected true to be nil or false
64 runs, 300 assertions, 1 failures, 0 errors, 0 skips
suai@tutorial:~/workspace/converse (gender-birthday) $
I am a newbie. Is this the correct way to do it?
Try This(#the failure is because you are checking with some space)
test "gender should be present" do
@user.is_female = ""
assert_not @user.valid?
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.