diff --git a/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb b/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb index 20bbf10df..789c15f45 100644 --- a/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb +++ b/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb @@ -99,16 +99,12 @@ def expected_enum_values hashify(options[:expected_enum_values]).with_indifferent_access end - def enum_method - attribute_name.to_s.pluralize - end - def actual_enum_values - model.class.send(enum_method) + model.class.send(attribute_name.to_s.pluralize) end def enum_defined? - model.class.respond_to?(enum_method) + model.defined_enums.keys.include?(attribute_name.to_s) end def enum_values_match? diff --git a/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb index 2639bf9bc..70c6e0140 100644 --- a/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb @@ -2,6 +2,14 @@ describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do if active_record_supports_enum? + it "rejects a record when checking for enum's plural attribute name" do + message = "Expected #{record_with_array_values.class} to define :#{enum_attribute.to_s.pluralize} as an enum" + + expect do + expect(record_with_array_values).to define_enum_for(enum_attribute.to_s.pluralize) + end.to fail_with_message(message) + end + describe "with only the attribute name specified" do it "accepts a record where the attribute is defined as an enum" do expect(record_with_array_values).to define_enum_for(enum_attribute)