1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Add optional(false) support to belong_to (#1237)

This commit is contained in:
Gui Vieira 2019-09-03 22:05:47 -07:00 committed by GitHub
parent 1424713162
commit ed962dee3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 10 deletions

View file

@ -1096,12 +1096,12 @@ module Shoulda
self
end
def optional
def optional(optional = true)
remove_submatcher(AssociationMatchers::RequiredMatcher)
add_submatcher(
AssociationMatchers::OptionalMatcher,
name,
true,
optional,
)
self
end

View file

@ -368,14 +368,14 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
end
end
context 'when qualified with optional' do
context 'when qualified with optional(true)' do
if active_record_supports_optional_for_associations?
context 'when belongs_to is configured to be required by default' do
it 'fails with an appropriate message' do
with_belongs_to_as_required_by_default do
assertion = lambda do
expect(belonging_to_parent).
to belong_to(:parent).optional
to belong_to(:parent).optional(true)
end
message = format_message(<<-MESSAGE, one_line: true)
@ -394,13 +394,62 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
context 'when belongs_to is not configured to be required by default' do
it 'passes' do
with_belongs_to_as_optional_by_default do
expect(belonging_to_parent).to belong_to(:parent).optional
expect(belonging_to_parent).to belong_to(:parent).optional(true)
end
end
end
else
it 'passes' do
expect(belonging_to_parent).to belong_to(:parent).optional
expect(belonging_to_parent).to belong_to(:parent).optional(true)
end
end
end
context 'when qualified with optional(false)' do
if active_record_supports_optional_for_associations?
context 'when belongs_to is configured to be required by default' do
it 'passes' do
with_belongs_to_as_required_by_default do
expect(belonging_to_parent).to belong_to(:parent).optional(false)
end
end
end
context 'when belongs_to is not configured to be required by default' do
it 'fails with an appropriate message' do
with_belongs_to_as_optional_by_default do
assertion = lambda do
expect(belonging_to_parent).
to belong_to(:parent).optional(false)
end
message = format_message(<<-MESSAGE, one_line: true)
Expected Child to have a belongs_to association called parent
(and for the record to fail validation if :parent is
unset; i.e., either the association should have been defined
with `optional: false`, or there should be a presence
validation on :parent)
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
else
it 'fails with an appropriate message' do
assertion = lambda do
expect(belonging_to_parent).
to belong_to(:parent).optional(false)
end
message = format_message(<<-MESSAGE, one_line: true)
Expected Child to have a belongs_to association called parent
(and for the record to fail validation if :parent is unset; i.e.,
either the association should have been defined with `optional:
false`, or there should be a presence validation on :parent)
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
@ -538,11 +587,11 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
end
end
context 'when qualified with optional' do
context 'when qualified with optional(true)' do
it 'fails with an appropriate message' do
assertion = lambda do
expect(belonging_to_parent(required: true)).
to belong_to(:parent).optional
to belong_to(:parent).optional(true)
end
message = format_message(<<-MESSAGE, one_line: true)
@ -557,6 +606,13 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
end
end
context 'when qualified with optional(false)' do
it 'passes' do
expect(belonging_to_parent(required: true)).
to belong_to(:parent).optional(false)
end
end
context 'when qualified with nothing' do
it 'passes' do
expect(belonging_to_parent(required: true)).to belong_to(:parent)
@ -591,10 +647,28 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
end
end
context 'when qualified with optional' do
context 'when qualified with optional(true)' do
it 'passes' do
expect(belonging_to_parent(optional: true)).
to belong_to(:parent).optional
to belong_to(:parent).optional(true)
end
end
context 'when qualified with optional(false)' do
it 'fails with an appropriate message' do
assertion = lambda do
expect(belonging_to_parent(optional: true)).
to belong_to(:parent).optional(false)
end
message = format_message(<<-MESSAGE, one_line: true)
Expected Child to have a belongs_to association called parent
(and for the record to fail validation if :parent is unset; i.e.,
either the association should have been defined with `optional:
false`, or there should be a presence validation on :parent)
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end