Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1096,12 +1096,12 @@ def required(required = true)
self
end

def optional
def optional(optional = true)
remove_submatcher(AssociationMatchers::RequiredMatcher)
add_submatcher(
AssociationMatchers::OptionalMatcher,
name,
true,
optional,
)
self
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@
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)
Expand All @@ -394,13 +394,62 @@
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
Expand Down Expand Up @@ -538,11 +587,11 @@
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)
Expand All @@ -557,6 +606,13 @@
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)
Expand Down Expand Up @@ -591,10 +647,28 @@
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

Expand Down