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

v4.4.0 NoMethodError: undefined method `belong_to' #1332

Closed
baburdick opened this issue Aug 26, 2020 · 12 comments
Closed

v4.4.0 NoMethodError: undefined method `belong_to' #1332

baburdick opened this issue Aug 26, 2020 · 12 comments

Comments

@baburdick
Copy link

With the release of v4.4.0, I'm seeing test failures with errors resembling this:

v4.4.0 NoMethodError: undefined method `belong_to' for #<RSpec::ExampleGroups::MyClass::Associations:0x00007fd5bdfc5518>

Reverting to the prior release fixes the issue.

@jarkko
Copy link

jarkko commented Aug 26, 2020

Ditto. Seems like pretty much all the association matchers broke for us.

  1) SaxoBlockTradeOrder is expected to have many :trade_orders
     Failure/Error: it { should have_many(:trade_orders) }
       expected #<SaxoBlockTradeOrder:0x00007f7eeb8b9ab8> to respond to `has_many?`
     # ./spec/models/saxo_block_trade_order_spec.rb:6:in `block (2 levels) in <main>'
     # -e:1:in `<main>'

  2) SaxoBlockTradeOrder 
     Failure/Error: it { should belong_to(:saxo_allocation_key) }
     
     NoMethodError:
       undefined method `belong_to' for #<RSpec::ExampleGroups::SaxoBlockTradeOrder:0x00007f7ee1154cc8>
     # ./spec/models/saxo_block_trade_order_spec.rb:5:in `block (2 levels) in <main>'
     # -e:1:in `<main>'

@hasrthur
Copy link

hasrthur commented Aug 26, 2020

Faced the same issue. Seems like the problem with autoloading. When just writing Shoulda::Matchers::ActiveRecord::AssociationMatcher and Shoulda::Matchers::ActiveRecord::AssociationMatchers before the tests resolve the issue but that's ugly

@denisj
Copy link

denisj commented Aug 26, 2020

Same here. So this might be this commit?

@n-rodriguez
Copy link

Same here. So this might be this commit?

why not using Zeitwerk?

@timlapluie
Copy link

timlapluie commented Aug 26, 2020

It seems to be a feature, not a bug.

The release notes of 4.4.0 contain the following:

Update have_many when used against a :through association so that it fails if the inverse model does not have a belongs_to association.

@Volosh1n
Copy link

@timlapluie but everything was fine in 4.3.0

@timlapluie
Copy link

@Volosh1n sorry, messed up the version number. This feature has been added with 4.4.0. Updated my comment above.

@hasrthur
Copy link

@timlapluie all the matchers don't work. Validations as well. Besides, have you read the error message in first comment? How come that undefined method is a proper error? Also, have you read my comment where the fix is pretty simple but stupid?

@jarkko
Copy link

jarkko commented Aug 26, 2020

It seems to be a feature, not a bug.

The release notes of 4.4.0 contain the following:

Update have_many when used against a :through association so that it fails if the inverse model does not have a belongs_to association.

Nope, this breaks all association matchers, not just through associations.

@KapilSachdev
Copy link
Collaborator

KapilSachdev commented Aug 26, 2020

Same here. So this might be this commit?

As @denisj mentioned, its because of the above mentioned commit change from require to autoloading the constants.
It would be good to just use 4.3.0 for now until fixed.
For verifying it just require association matchers and similar files in your spec_helper in case using rails.

require "shoulda/matchers/active_record/association_matcher"	
require "shoulda/matchers/active_record/association_matchers"	
require "shoulda/matchers/active_record/association_matchers/counter_cache_matcher"	
require "shoulda/matchers/active_record/association_matchers/inverse_of_matcher"	
require "shoulda/matchers/active_record/association_matchers/join_table_matcher"	
require "shoulda/matchers/active_record/association_matchers/order_matcher"	
require "shoulda/matchers/active_record/association_matchers/through_matcher"	
require "shoulda/matchers/active_record/association_matchers/dependent_matcher"	
require "shoulda/matchers/active_record/association_matchers/required_matcher"	
require "shoulda/matchers/active_record/association_matchers/optional_matcher"	
require "shoulda/matchers/active_record/association_matchers/source_matcher"	
require "shoulda/matchers/active_record/association_matchers/model_reflector"	
require "shoulda/matchers/active_record/association_matchers/model_reflection"	
require "shoulda/matchers/active_record/association_matchers/option_verifier"

or just add Shoulda::Matchers::ActiveRecord::AssociationMatcher anywhere before the test so that matchers get loaded.

Just for verification

@vsppedro
Copy link
Collaborator

Thank you all for the information shared here.

I will send a correction as soon as possible.

@mcmire
Copy link
Collaborator

mcmire commented Aug 26, 2020

Fixed in v4.4.1. Sorry everybody!

@mcmire mcmire closed this as completed Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants