Skip to content

Commit

Permalink
fix have_db_column.with_options misspelled options
Browse files Browse the repository at this point in the history
have_db_column().with_options() should raise an ArgumentError
if receives a misspelled argument
  • Loading branch information
rodriggochaves committed Oct 2, 2020
1 parent 256d3b5 commit c5b93bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/shoulda/matchers/active_record/have_db_column_matcher.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit c5b93bf

Please sign in to comment.