From c5b93bf4b727a925a26c7cbd5710d2f1947cd7a3 Mon Sep 17 00:00:00 2001 From: Rodrigo A Chaves Date: Thu, 1 Oct 2020 21:43:03 -0300 Subject: [PATCH] fix have_db_column.with_options misspelled options have_db_column().with_options() should raise an ArgumentError if receives a misspelled argument --- .../matchers/active_record/have_db_column_matcher.rb | 10 +++++++++- .../active_record/have_db_column_matcher_spec.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/shoulda/matchers/active_record/have_db_column_matcher.rb b/lib/shoulda/matchers/active_record/have_db_column_matcher.rb index 76f955fa2..d60c645b2 100644 --- a/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +++ b/lib/shoulda/matchers/active_record/have_db_column_matcher.rb @@ -95,7 +95,8 @@ def of_type(column_type) end def with_options(opts = {}) - %w(precision limit default null scale primary).each do |attribute| + validate_options(opts) + OPTIONS.each do |attribute| if opts.key?(attribute.to_sym) @options[attribute.to_sym] = opts[attribute.to_sym] end @@ -137,6 +138,13 @@ def description protected + OPTIONS = %i(precision limit default null scale primary) + + def validate_options(opts) + invalid_options = opts.keys.map(&:to_sym) - OPTIONS + raise ArgumentError, "Unknown option(s) '#{invalid_options}'" unless invalid_options.empty? + end + def column_exists? if model_class.column_names.include?(@column.to_s) true diff --git a/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb index 1d23259a1..8933c703b 100644 --- a/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb @@ -98,6 +98,14 @@ end end + context 'with invalid argument option' do + it 'throws an ArgumentError' do + expect { + have_db_column(:salary).with_options(preccision: 5) + }.to raise_error(ArgumentError) + end + end + def model(options = {}) define_model(:employee, options).new end