如何为布尔值的存在和有效性编写测试?
分贝/迁移/ [时间戳] _add_admin_to_users.rb
add_column :users, :is_female, :boolean, default: false
应用程序/模型/ user.rb
validates_inclusion_of :is_female, in: [true, false]
测试/模型/ 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
我收到以下错误。
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) $
我是新手。这是正确的方法吗?
答案 0 :(得分:1)
试试这个(#失败是因为你正在检查一些空间)
test "gender should be present" do
@user.is_female = ""
assert_not @user.valid?
end