Shoulda belongs_to with class_name and foreign_key

You should be able to use:

it { should belong_to(:owner).class_name('Person') }

Shoulda's belong_to matcher always reads the foreign_key from the association and tests that it is a valid field name, so you don't need to do anything more.

(See Shoulda::Matchers::ActiveRecord::AssociationMatcher#foreign_key_exists? and associated methods)


Now it's possible to test custom foreign keys:

it { should belong_to(:owner).class_name('Person').with_foreign_key('person_id') }

See : https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_record/association_matcher.rb#L122