diff --git a/NEWS.md b/NEWS.md index d38ef1b9..0ee30ab9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -70,6 +70,11 @@ is now: * *PR: [#1073]* * *Original issue: [#949]* +* Fix `validate_inclusion_of` so that if it fails, it will no longer blow up + with the error "undefined method \`attribute_setter' for nil:NilClass". + + * *Original issue: [#904]* + ### Features * Add `required` and `optional` qualifiers to `belong_to` and `have_one` @@ -143,6 +148,7 @@ is now: [#961]: https://github.com/thoughtbot/shoulda-matchers/issues/961 [795ca68]: https://github.com/thoughtbot/shoulda-matchers/commit/795ca688bff08590dbd2ab6f2b51ea415e0c7473 [#1089]: https://github.com/thoughtbot/shoulda-matchers/pulls/1089 +[#904]: https://github.com/thoughtbot/shoulda-matchers/issues/904 ### Improvements diff --git a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb index f152cb54..6997f950 100644 --- a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb @@ -465,8 +465,8 @@ EOT end end - !values_outside_of_array.any? do |value| - allows_value_of(value, @low_message) + values_outside_of_array.all? do |value| + disallows_value_of(value, @low_message) end end diff --git a/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb index a5efb15e..b47f8ec5 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb @@ -448,9 +448,21 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode define_method(:valid_values) { args.fetch(:possible_values) } - it 'does not match a record with no validations' do - builder = build_object - expect_not_to_match_on_values(builder, possible_values) + context 'when the record has no validations' do + it 'passes when used in the negative' do + builder = build_object + expect_not_to_match_on_values(builder, possible_values) + end + + it 'fails when used in the positive with an appropriate failure message' do + builder = build_object + + assertion = lambda do + expect_to_match_on_values(builder, possible_values) + end + + expect(&assertion).to fail + end end it 'matches given the same array of valid values' do diff --git a/spec/unit_spec_helper.rb b/spec/unit_spec_helper.rb index 26c832ad..7bc23e6c 100644 --- a/spec/unit_spec_helper.rb +++ b/spec/unit_spec_helper.rb @@ -1,6 +1,7 @@ require_relative 'support/unit/load_environment' require 'rspec/rails' +require 'rspec/matchers/fail_matchers' require 'shoulda-matchers' require 'spec_helper' @@ -10,6 +11,8 @@ Dir[ File.join(File.expand_path('../support/unit/**/*.rb', __FILE__)) ].sort.eac end RSpec.configure do |config| + config.include RSpec::Matchers::FailMatchers + UnitTests::ActionPackVersions.configure_example_group(config) UnitTests::ActiveModelHelpers.configure_example_group(config) UnitTests::ActiveModelVersions.configure_example_group(config)