扶手:一个布尔值的验证失败纳入测试 [英] Rails: Validating inclusion of a boolean fails tests

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

问题描述

我想确保我的模型的字段是一个布尔值,但我的测试中继续失败。

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

看完这个:<一href=\"http://stackoverflow.com/questions/3648619/validating-boolean-value-in-rspec-and-rails\">Validating在Rspec的和Rails 布尔值,这个值<一个href=\"http://stackoverflow.com/questions/3608076/rails-how-do-i-validate-that-something-is-a-boolean\">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的和早该匹配器),并从我的测试继续失败,我现在到了最愚蠢的可能(?)的方式。尽管如此,测试没通过,我猜有一些机制,某处转换值。

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

我缺少什么?
似乎值转换的有点逻辑的方式,但在哪里以及如何prevent呢?如何正确地做这种验证?

推荐答案

我不知道这个想法,你需要验证一个布尔字段为真/假从何而来,但我已经在几个不相关的看到它最近的项目,所以我开始怀疑这不是某个地方一个米姆。

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.

查看源,这里的code,它的值转换为布尔:

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

所以,我们再看一下你的code以上,一切都是理所应当的,但你期待什么不正确。分配富到一个布尔值,我们看到,富是不是在可以接受的真值的列表,所以布尔字段默认为false,这是它会从它的存取稍后再回来。该字段的布尔值仍:在= GT; [真,假] 这样的模式是有效的。 假和123失败出于同样的原因。 真是布尔的一个可接受的值为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.

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

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