Ruby on rails Rails 4 rspec测试仅在数值上失败\u布尔值上的整数?

Ruby on rails Rails 4 rspec测试仅在数值上失败\u布尔值上的整数?,ruby-on-rails,ruby,rspec,ruby-on-rails-4,Ruby On Rails,Ruby,Rspec,Ruby On Rails 4,我的模型里有 validates :is_foo, presence: true, numericality: {only_integer:true, less_than_or_equal_to: 1, greater_than_or_equal_to: 0} 按照我的要求 describe "when is_foo is not an integer" do before {@user.is_foo = true} it {should_not be_valid} e

我的模型里有

validates :is_foo, presence: true, numericality: {only_integer:true, less_than_or_equal_to: 1, greater_than_or_equal_to: 0}
按照我的要求

  describe "when is_foo is not an integer" do
    before {@user.is_foo = true}
    it {should_not be_valid}
  end
上面的测试失败了,但是由于我将is_foo设置为布尔值而不是整数,测试应该通过吗

布尔在ruby中被认为是整数吗?因为当我这么做的时候

true.to_i # error
true == 1 # false
false == 0 # false

规格是正确的。1是truthy(Ruby中唯一的非truthy值是false和nil),但它不是布尔值。布尔不是数字;他们是他们自己的类型。数值性验证将验证类型是否为实际数值,布尔值是否为非数值

> true.class
=> TrueClass
> 1.class
=> Fixnum
不过,您可能需要的是测试布尔值的验证:

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

当模型类属性需要整数时,我猜ruby/rails会将1替换为true。因此测试失败,因为它将1与整数进行比较