thoughtbot--shoulda-matchers/spec
Elliot Winkler a465e796e8 Fix default behavior of belong_to under Rails 4.2
Rails 5 introduced a change where `belongs_to` would default to adding a
presence validation along with the association. However, it also
introduced a configuration option, `belongs_to_required_by_default`, to
emulate the old behavior prior to Rails 5. For Rails 4.2 projects as
well as Rails 5 which were migrated from 4, this setting is false, so
that existing apps do not break.

To mimic this, a change was made to the `belong_to` matcher to check for
the presence of the presence validator if
`belongs_to_required_by_default` is true and check for the absence of
the presence validator if it is false. However, this last bit of the
logic actually causes problems. Take this case, for example:

    ActiveRecord::Base.belongs_to_required_by_default = false

    class Post < ActiveRecord::Base
      belongs_to :user
      validates :user, presence: true
    end

    RSpec.describe Post, type: :model do
      it { is_expected.to belong_to(:user) }
    end

In this example, the developer has chosen to place a presence validation
on the association manually. `belong_to` doesn't know this, however, and
will check to make sure that `user` can be nil, which of course it
can't. Therefore, this test will fail. In addition, the failure message
that `belong_to` generates is confusing:

    Expected Post to have a belongs_to association called user (the
    association should have been defined with `optional: true`, but was
    not)

The reason why the test fails is that when
`belongs_to_required_by_default` is false, belong_to` will place an
implicit `optional` qualifier on itself. In other words, these two tests
are equivalent:

    it { is_expected.to belong_to(:user) }
    it { is_expected.to belong_to(:user).optional }

However, this is not only wrong, but the `belongs_to` macro in Rails 4.2
doesn't have an `optional` option (it has `required` instead), so the
failure message that `belong_to` generates is confusing.

This commit fixes this by modifying `belong_to` so that under Rails 4.2,
the matcher will have not have any qualifiers on it by default.
2018-09-16 12:21:41 -06:00
..
acceptance Adding support to allow_nil option for delegate_method matcher 2018-01-24 02:22:19 -06:00
support Fix default behavior of belong_to under Rails 4.2 2018-09-16 12:21:41 -06:00
unit/shoulda/matchers Fix default behavior of belong_to under Rails 4.2 2018-09-16 12:21:41 -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 Update rspec & rspec-rails to 3.6.x 2017-09-17 17:01:50 -05:00
unit_spec_helper.rb Fix inclusion to correctly disallow outside values 2018-09-15 13:43:30 -03:00
warnings_spy.rb Fail build if there are warnings 2015-02-09 10:48:49 -07:00