thoughtbot--shoulda-matchers/spec
Mauro George c0a1578435 Fail have_many :through when inverse is invalid
When used with a `:through` association where a corresponding
`belongs_to` association has not been set up on the inverse model, the
`have_many` matcher raises this error:

    Failures:

      1) User should have many projects through clients
         Failure/Error: it {  should have_many(:projects).through :clients }
         NoMethodError:
           undefined method `class_name' for nil:NilClass
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:871:in `derive_class_name'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:147:in `class_name'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:1067:in `rescue in class_exists?'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:1064:in `class_exists?'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:928:in `matches?'
         # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

Fortunately, ActiveRecord has a `check_validity!` method we can call on
an association. For `:through` associations, this will run through a
litany of checks, one of which is to check for the inverse association,
which we want in this case. We rescue the error that is produced and
include this in the failure message.

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
2020-07-10 22:44:22 -06:00
..
acceptance Update test versions and dependencies (#1269) 2020-01-01 12:20:50 -08:00
support Fix spec runner: read command output during run 2020-05-12 23:07:16 -06:00
unit/shoulda/matchers Fail have_many :through when inverse is invalid 2020-07-10 22:44:22 -06:00
warnings_spy Be better about looking for warnings on Travis 2014-12-16 20:30:05 -07:00
acceptance_spec_helper.rb Extract a common spec helper 2015-10-07 23:32:51 -06:00
doublespeak_spec_helper.rb Extract a common spec helper 2015-10-07 23:32:51 -06:00
report_warnings.rb Remove all Ruby-emitted warnings 2014-07-18 18:00:08 -06:00
spec_helper.rb Only turn on VERBOSE after requiring shoulda-matchers 2019-03-20 22:44:06 -07:00
unit_spec_helper.rb Remove code for unsupported versions (#1270) 2020-01-01 19:10:35 -08:00
warnings_spy.rb Fail build if there are warnings 2015-02-09 10:48:49 -07:00