How can I test :inclusion validation in Rails using RSpec

前端 未结 3 1025
终归单人心
终归单人心 2021-01-31 15:14

I have the following validation in my ActiveRecord.

validates :active, :inclusion => {:in => [\'Y\', \'N\']}

I am using the following to

3条回答
  •  旧巷少年郎
    2021-01-31 15:57

    Use shoulda_matchers

    In recent versions of shoulda-matchers (at least as of v2.7.0), you can do:

    expect(subject).to validate_inclusion_of(:active).in_array(%w[Y N])
    

    This tests that the array of acceptable values in the validation exactly matches this spec.

    In earlier versions, >= v1.4 , shoulda_matchers supports this syntax:

    it {should ensure_inclusion_of(:active).in_array(%w[Y N]) }
    

提交回复
热议问题