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

时间:2013-11-05 17:20:10

标签: ruby-on-rails ruby testing rspec shoulda

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

# 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

失败:

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)>'

厂:

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

我省略了其他几个属性,但没有什么特别之处,它们不会影响代码。

2 个答案:

答案 0 :(得分:2)

这似乎是a bug with shoulda-matchers。建议的解决方法是使用allow_value匹配器。

答案 1 :(得分:1)

我认为您的:published属性为 nil ,被视为false,但它不是您拥有的false值你的阵列。