thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.51k stars 912 forks source link

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

Closed baburdick closed 4 years ago

baburdick commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Same here. So this might be this commit?

n-rodriguez commented 4 years ago

Same here. So this might be this commit?

why not using Zeitwerk?

timlapluie commented 4 years ago

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 commented 4 years ago

@timlapluie but everything was fine in 4.3.0

timlapluie commented 4 years ago

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

hasrthur commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Thank you all for the information shared here.

I will send a correction as soon as possible.

mcmire commented 4 years ago

Fixed in v4.4.1. Sorry everybody!