1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/unit/shoulda
Elliot Winkler 707d87dbd6 Add without_presence_validation q to belong_to
With the new Rails 5 behavior, `belong_to` will check to ensure that the
association has a presence validation on it. In some cases, however,
this is not desirable. For instance, say we have this setup:

    class Employee < ApplicationRecord
      # Assume belongs_to_required_by_default is true
      belongs_to :manager

      before_validation :add_manager

      private

      def add_manager
        self.manager = Manager.create
      end
    end

In this case, even though the association is effectively defined with
`required: true`, the ensuing presence validation never fails, because
`manager` is always set to something before validations kick off. So
this test won't work:

    it { should belong_to(:manager) }

To get around this, this commit allows us to say:

    it { should belong_to(:manager).without_presence_validation }

which instructs the matcher not to test for any presence (or absence,
for that matter) of a presence validation, mimicking the pre-Rails 5
behavior.
2019-02-14 17:48:26 -07:00
..
matchers Add without_presence_validation q to belong_to 2019-02-14 17:48:26 -07:00