Rails:验证是否包含布尔值失败测试 [英] Rails: Validating inclusion of a boolean fails tests

查看:13
本文介绍了Rails:验证是否包含布尔值失败测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确保我的模型的字段是布尔值,但我的测试一直失败.

I'm trying to ensure that a field of my model is a boolean, but my tests keep on failing.

阅读后:在 Rspec 和 Rails 中验证布尔值而这个 Rails: 我如何验证某事是布尔值?我最终这样做了:

After reading this: Validating boolean value in Rspec and Rails and this Rails: how do I validate that something is a boolean? I ended up doing it like so:

class Model < ActiveRecord::Base

  validates :my_field, :inclusion => { :in => [true, false] }

end

我尝试了几种不同的方法(使用 rspec 和 shoulda 匹配器)来测试它,由于我的测试不断失败,我现在只能采用最愚蠢的 (?) 方法.不过,测试没有通过,我猜有某种机制可以在某处转换值.

I've tried testing this a few different ways (using rspec and shoulda matchers) and since my tests keep on failing, I'm right now down to the dumbest possible (?) way. Still, the tests don't pass and I'm guessing that there's some mechanism that converts the value somewhere.

这是我用来了解发生了什么的:

Here's what I'm using to find out what's going on:

# create instance without setting value ...

# these work as expected
model_instance.valid?.should be_false      # passes
model_instance.my_field = true
model_instance.valid?.should be_true       # passes
model_instance.my_field = false       
model_instance.valid?.should be_true       # passes

# works as expected
model_instance.my_field = ""
model_instance.valid?.should be_false      # passes

# these should pass but fail
model_instance.my_field = "foo"
model_instance.my_field.should == "foo"    # fails as well, my_field == false
model_instance.valid?.should be_false      # fails

model_instance.my_field = "false"
model_instance.my_field.should == "false"  # fails as well, my_field == false
model_instance.valid?.should be_false      # fails

model_instance.my_field = "123"
model_instance.valid?.should be_false      # fails

model_instance.my_field = "true"
model_instance.my_field.should == "true"   # fails as well, my_field == true
model_instance.valid?.should be_false      # fails

我错过了什么?似乎该值以某种合乎逻辑的方式转换,但在哪里以及如何防止它?如何正确进行这种验证?

推荐答案

我不知道你需要验证布尔字段为真/假的想法来自哪里,但我在一些不相关的地方看到了它最近的项目,所以我开始怀疑这是否不是从某个地方开始的模因.

I don't know where this idea that you need to validate a boolean field as true/false came from, but i've seen it in a few unrelated projects recently so I'm starting to wonder if this isn't a meme that started somewhere.

它是一个布尔字段,它必须为真或假.AR 为您处理布尔值的实现细节.如果您想要一个布尔字段,请在数据库中创建一个布尔字段.故事结局.

Its a boolean field, it HAS to be true or false. AR takes care of the implementation details of boolean values for you. If you want a boolean field, create a boolean field in the database. End of story.

查看源代码,这是将值转换为布尔值的代码:

Looking at the source, here's the code that converts a value to a boolean:

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 147
147:         def value_to_boolean(value)
148:           if value.is_a?(String) && value.blank?
149:             nil
150:           else
151:             TRUE_VALUES.include?(value)
152:           end
153:         end

以下是相当于布尔值的值:

And here are the values that equate to their boolean counterparts:

TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set

所以再看看你上面的代码,一切都应该是这样,除了你所期望的不正确.将foo"分配给一个布尔值,我们看到foo"不在可接受的真值列表中,所以布尔字段默认为 false,这就是它稍后将从其访问器返回的值.该字段的布尔值仍然是 :in =>;[true, false] 所以模型是有效的.false"和123"因同样的原因而失败.true"是布尔值 true 的可接受值,因此该字段设置为 true,并且由于上述原因仍然有效.

So taking another look at your code above, everything is as it should be, except that what you're expecting is incorrect. assigning "foo" to a boolean value we see that "foo" is not in the list of acceptable true values, so the boolean field defaults to false, which is what it will return from its accessor later. The boolean value for the field is still :in => [true, false] so the model is valid. "false" and "123" fail for the same reason. "true" is an acceptable value of the boolean true, so the field gets set to true and is still valid for the reason outlined above.

这篇关于Rails:验证是否包含布尔值失败测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆