From 5e1e531b8b3fd35a526e73465bd4de64c22a1763 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 24 Jul 2017 23:09:20 -0500 Subject: [PATCH] Rails 5: Fix failing association matcher tests --- .../association_matchers/option_verifier.rb | 17 +++++++++++------ .../unit/helpers/active_record_versions.rb | 8 ++++++++ .../active_record/association_matcher_spec.rb | 11 +++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb b/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb index 999feb7a..3ad93b76 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb @@ -34,15 +34,16 @@ def correct_for_relation_clause?(name, expected_value) def correct_for?(*args) expected_value, name, type = args.reverse + if expected_value.nil? true else - expected_value = type_cast( + type_cast_expected_value = type_cast( type, expected_value_for(type, name, expected_value) ) actual_value = type_cast(type, actual_value_for(name)) - expected_value == actual_value + type_cast_expected_value == actual_value end end @@ -65,10 +66,14 @@ def actual_value_for(name) def type_cast(type, value) case type - when :string, :relation_clause then value.to_s - when :boolean then !!value - when :hash then Hash(value).stringify_keys - else value + when :string, :relation_clause + value.to_s + when :boolean + !!value + when :hash + Hash(value).stringify_keys + else + value end end diff --git a/spec/support/unit/helpers/active_record_versions.rb b/spec/support/unit/helpers/active_record_versions.rb index 94e21f2e..d682b235 100644 --- a/spec/support/unit/helpers/active_record_versions.rb +++ b/spec/support/unit/helpers/active_record_versions.rb @@ -20,5 +20,13 @@ def active_record_supports_has_secure_password? def active_record_supports_array_columns? active_record_version > 4.2 end + + def active_record_supports_relations? + active_record_version >= 4 + end + + def active_record_supports_more_dependent_options? + active_record_version >= 4 + end end end diff --git a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb index b51f9b4e..f383487a 100644 --- a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb @@ -1210,7 +1210,7 @@ def having_and_belonging_to_many_non_existent_class(model_name, assoc_name, opti def define_association_with_conditions(model, macro, name, conditions, other_options={}) args = [] options = {} - if Shoulda::Matchers::RailsShim.active_record_major_version == 4 + if active_record_supports_relations? args << proc { where(conditions) } else options[:conditions] = conditions @@ -1222,7 +1222,7 @@ def define_association_with_conditions(model, macro, name, conditions, other_opt def define_association_with_order(model, macro, name, order, other_options={}) args = [] options = {} - if Shoulda::Matchers::RailsShim.active_record_major_version == 4 + if active_record_supports_relations? args << proc { order(order) } else options[:order] = order @@ -1232,11 +1232,10 @@ def define_association_with_order(model, macro, name, order, other_options={}) end def dependent_options - case Rails.version - when /\A3/ - [:destroy, :delete, :nullify, :restrict] - when /\A4/ + if active_record_supports_more_dependent_options? [:destroy, :delete, :nullify, :restrict_with_exception, :restrict_with_error] + else + [:destroy, :delete, :nullify, :restrict] end end end