Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with_foreign_key modifier fails with no error message #1374

Closed
orenyk opened this issue Nov 6, 2020 · 3 comments · Fixed by #1376
Closed

with_foreign_key modifier fails with no error message #1374

orenyk opened this issue Nov 6, 2020 · 3 comments · Fixed by #1376
Assignees

Comments

@orenyk
Copy link

orenyk commented Nov 6, 2020

Steps to replicate:

# migration
create_table :foos do |t|
  t.references :bar, foreign_key: true
  t.timestamps
end

create_table :bars do |t|
  t.timestamps
end

# app/model/foo.rb
class Foo < ApplicationRecord
  belongs_to :pub, class_name: 'Bar', foreign_key: :bar_id
end

# app/model/bar.rb
class Bar < ApplicationRecord
  has_many :foos, inverse_of: :pub
end

# spec/models/foo_spec.rb
...
it { is_expected.to belong_to(:pub).class_name('Bar').with_foreign_key(:bar_id) }
...

The test passes but if I change the argument of with_foreign_key to anything else (e.g. baz_id) the spec fails with the following error message:

Expected Foo to have a belongs_to association called pub ()

If the class_name argument is changed to be incorrect there is a specific error message in the parentheses for the submatcher (pub should resolve to Baz for class_name), so I'm guessing that somehow the following lines are no longer working as expected:

else
@missing = "#{klass} does not have a #{foreign_key} foreign key."
false
end

Hope that helps!

@orenyk orenyk changed the title with_foreign_key modifier fails with no error message with_foreign_key modifier fails with no error message Nov 6, 2020
@mcmire
Copy link
Collaborator

mcmire commented Nov 6, 2020

Dang, okay. These association matchers could use some better error handling so I'm not surprised. But thank you for the report!

@vsppedro
Copy link
Collaborator

Hi, @orenyk , thank you very much for all this information. The description of this issue helped me a lot.

This fix will be available on the next release. It's already on the master.

Thanks again!

@orenyk
Copy link
Author

orenyk commented Nov 20, 2020

Awesome, thanks! I meant to take a look myself but ran out of time 😄. Appreciate the heads up too, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants