Shoulda have_many with class_name, foreign_key and primary_key
<p>How do I test this ActiveRecord relation using ShouldaMatchers?</p>

<h2>Models</h2>

<pre><code>class ViolatorUnitHistory &lt; 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 &lt; ActiveRecord::Base
  has_many :violator_unit_histories
end
</code></pre>

<h2>Test</h2>

<pre><code>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
</code></pre>

<h2>Current Results</h2>

<pre><code>Failures:
  1) Source relationships should have many violator_unit_histories class_name =&gt; 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 &lt;top (required)&gt;'
</code></pre>

<h2>Previous Results</h2>

<pre><code>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.)
</code></pre>

<p>I saw the answer posted <a href="https://stackoverflow.com/questions/21657906/shoulda-matchers-have-many-with-custom-relation-name">here</a> for the <code>belongs_to</code> side of the test. But I can't seem to figure out he <code>has_many</code> side to these more complex tests.</p>