How do I test this ActiveRecord relation using ShouldaMatchers?
Models
class ViolatorUnitHistory < ActiveRecord::Base
...
belongs_to :primary_source, class_name: Source, primary_key: :source_1_id, foreign_key: :id
belongs_to :secondary_source, class_name: Source, primary_key: :source_2_id, foreign_key: :id
belongs_to :tertiary_source, class_name: Source, primary_key: :source_3_id, foreign_key: :id
...
end
class Source < ActiveRecord::Base
has_many :violator_unit_histories
end
Test
describe "relationships" do
# Can't figure out this relationship
it { is_expected.to have_many(:violator_unit_histories).class_name('Source').with_primary_key('source_1_id').with_foreign_key('id') }
end
Current Results
Failures:
1) Source relationships should have many violator_unit_histories class_name => Source
Failure/Error: it { is_expected.to have_many(:violator_unit_histories).class_name('Source').with_primary_key('source_1_id').with_foreign_key('id') }
Expected Source to have a has_many association called violator_unit_histories ()
# ./spec/models/source_spec.rb:17:in `block (3 levels) in <top (required)>'
Previous Results
it { is_expected.to have_many(:violator_unit_histories) }
Failures:
1) Source relationships should have many violator_unit_histories
Failure/Error: it { is_expected.to have_many(:violator_unit_histories) }
Expected Source to have a has_many association called violator_unit_histories (ViolatorUnitHistory does not have a source_id foreign key.)
I saw the answer posted here for the belongs_to
side of the test. But I can't seem to figure out he has_many
side to these more complex tests.