tardjo
tardjo

Reputation: 1654

how to make validates inclusion true or false work in testing rails?

how to make validates inclusion true or false work in testing rails?

i'm using gem shoulda for my testing, if i have some validates like this in my model :

class Draw < ActiveRecord::Base    
    validates :available, inclusion: { in: [true, false] }
end

how to make this validate work in testing?, when i try this code in my model testing :

require 'test_helper'

class DrawTest < ActiveSupport::TestCase    
  should ensure_inclusion_of(:available).in_array([true, false])
end

i get error like this :

DrawTest#test_: Draw should ensure inclusion of available in [true, false].  [/home/my_user/.rvm/gems/ruby-2.0.0-p451/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:344]:
[true, false] doesn't match array in validation

how to resove this problem?

Upvotes: 2

Views: 2109

Answers (2)

Thadeu Esteves Jr.
Thadeu Esteves Jr.

Reputation: 403

I solved problem with like this code.

it { should allow_value(%w(true false)).for(:done) }

Upvotes: 3

infused
infused

Reputation: 24357

Under version 2.6.2, should-matchers issues the following warning when testing ensure_inclusion_of(:available).in_array([true, false]):

Warning from shoulda-matchers:

You are using `ensure_inclusion_of` to assert that a boolean column allows
boolean values and disallows non-boolean ones. Assuming you are using
`validates_format_of` in your model, be aware that it is not possible to fully
test this, and in fact the validation is superfluous, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you should
consider removing this test and the corresponding validation.

It seems that you should just remove the test and validation.

Upvotes: 3

Related Questions