为什么在数组中包含值的测试失败了?

时间:2021-07-26 18:58:58

The following test is always failing, but it's not clear why.

以下测试总是失败,但目前尚不清楚原因。

# entry_spec.rb
require 'spec_helper'

describe Entry do
  before { @entry = build_stubbed :entry }
  subject { @entry }

  it { should respond_to :published }

  describe 'validation' do
    it { should ensure_inclusion_of(:published).in_array([true, false]) }
  end
end

# entry.rb
class Entry < ActiveRecord::Base
  validates :published, inclusion: { in: [true, false] }
end

The failure:

失败:

1) Entry validation should ensure inclusion of published in [true, false]
     Failure/Error: it { should ensure_inclusion_of(:published).in_array([true, false]) }
       [true, false] doesn't match array in validation
     # ./spec/models/entry_spec.rb:18:in `block (3 levels) in <top (required)>'

Factory:

厂:

FactoryGirl.define do
  factory :entry do
    published true
  end
end

I'm ommitted a couple of other attributes, but there is nothing special going on there, and they shouldn't impact the code.

我省略了其他一些属性,但没有什么特别之处,它们不应该影响代码。

2 个解决方案

#1


2  

This appears to be a bug with shoulda-matchers. The suggested workaround is to use allow_value matcher instead.

这似乎是与shoulda-matchers的错误。建议的解决方法是使用allow_value匹配器。

#2


1  

I suppose your :published attribute is nil which is treated as false but it isn't the explicit false value you have in your array.

我想你的:发布属性是nil,被视为false,但它不是你的数组中的显式false值。

#1


2  

This appears to be a bug with shoulda-matchers. The suggested workaround is to use allow_value matcher instead.

这似乎是与shoulda-matchers的错误。建议的解决方法是使用allow_value匹配器。

#2


1  

I suppose your :published attribute is nil which is treated as false but it isn't the explicit false value you have in your array.

我想你的:发布属性是nil,被视为false,但它不是你的数组中的显式false值。