fix: with_foreign_key modifier fails with wrong error message (#1376)
This commit is contained in:
parent
8772cd41a6
commit
64376a0bcd
|
@ -1394,19 +1394,28 @@ module Shoulda
|
||||||
end
|
end
|
||||||
|
|
||||||
def class_has_foreign_key?(klass)
|
def class_has_foreign_key?(klass)
|
||||||
if options.key?(:foreign_key)
|
if options.key?(:foreign_key) && !foreign_key_correct?
|
||||||
option_verifier.correct_for_string?(
|
@missing = foreign_key_failure_message(klass, options[:foreign_key])
|
||||||
:foreign_key,
|
|
||||||
options[:foreign_key],
|
|
||||||
)
|
|
||||||
elsif column_names_for(klass).include?(foreign_key)
|
|
||||||
true
|
|
||||||
else
|
|
||||||
@missing = "#{klass} does not have a #{foreign_key} foreign key."
|
|
||||||
false
|
false
|
||||||
|
elsif !column_names_for(klass).include?(foreign_key)
|
||||||
|
@missing = foreign_key_failure_message(klass, foreign_key)
|
||||||
|
false
|
||||||
|
else
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def foreign_key_correct?
|
||||||
|
option_verifier.correct_for_string?(
|
||||||
|
:foreign_key,
|
||||||
|
options[:foreign_key],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def foreign_key_failure_message(klass, foreign_key)
|
||||||
|
"#{klass} does not have a #{foreign_key} foreign key."
|
||||||
|
end
|
||||||
|
|
||||||
def primary_key_correct?(klass)
|
def primary_key_correct?(klass)
|
||||||
if options.key?(:primary_key)
|
if options.key?(:primary_key)
|
||||||
if option_verifier.correct_for_string?(
|
if option_verifier.correct_for_string?(
|
||||||
|
|
|
@ -1414,6 +1414,20 @@ Expected Parent to have a has_many association called children through conceptio
|
||||||
expect(having_one_non_existent(:person, :detail, class_name: 'Detail')).not_to have_one(:detail)
|
expect(having_one_non_existent(:person, :detail, class_name: 'Detail')).not_to have_one(:detail)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'rejects an association with a valid :class_name and a bad :primary_key option' do
|
||||||
|
define_model :person_detail
|
||||||
|
define_model :person do
|
||||||
|
has_one :detail, class_name: 'PersonDetail', foreign_key: :person_detail_id
|
||||||
|
end
|
||||||
|
|
||||||
|
expected_message = 'Expected Person to have a has_one association called detail ' \
|
||||||
|
'(PersonDetail does not have a custom_primary_id foreign key.)'
|
||||||
|
|
||||||
|
expect { have_one(:detail).class_name('PersonDetail').with_foreign_key(:custom_primary_id) }.
|
||||||
|
not_to match_against(Person.new).
|
||||||
|
and_fail_with(expected_message)
|
||||||
|
end
|
||||||
|
|
||||||
it 'adds error message when rejecting an association with non-existent class' do
|
it 'adds error message when rejecting an association with non-existent class' do
|
||||||
message = 'Expected Person to have a has_one association called detail (Detail2 does not exist)'
|
message = 'Expected Person to have a has_one association called detail (Detail2 does not exist)'
|
||||||
expect {
|
expect {
|
||||||
|
|
Loading…
Reference in New Issue