1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
This commit is contained in:
Elliot Winkler 2019-06-10 23:54:38 -06:00
parent 4708ee2089
commit b46db9a546
2 changed files with 160 additions and 1 deletions

View file

@ -1061,11 +1061,13 @@ module Shoulda
self
end
# TODO: this should default to true
def autosave(autosave)
@options[:autosave] = autosave
self
end
# TODO: this should default to true
def index_errors(index_errors)
@options[:index_errors] = index_errors
self
@ -1096,6 +1098,7 @@ module Shoulda
self
end
# TODO: optional: false
def optional
remove_submatcher(AssociationMatchers::RequiredMatcher)
add_submatcher(

View file

@ -404,7 +404,19 @@ Expected Child to have a belongs_to association called parent (parent should hav
end
context 'when the parent model does not have the inverse association' do
it 'fails'
it 'fails' do
pending "This won't work"
it 'does not match' do
record = record_belonging_to(:parent, model_name: 'Child')
expect { belong_to(:parent).inverse_of(:children) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (Parent does not have an association :children)
MESSAGE
end
end
end
end
@ -594,6 +606,150 @@ Expected Child to have a belongs_to association called parent (parent should res
end
end
context 'qualified with autosave' do
context 'when the association has been configured with autosave: true' do
it 'matches' do
pending 'TODO'
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: true,
)
expect { belong_to(:parent).autosave }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect Child to have a belongs_to association called parent
MESSAGE
end
end
context 'when the association has been configured with autosave: false' do
it 'does not match' do
pending 'TODO'
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: false,
)
expect { belong_to(:parent).autosave }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave => true)
MESSAGE
end
end
context 'when the association has not been configured with :autosave at all' do
it 'does not match' do
pending 'TODO'
record = record_belonging_to(:parent, model_name: 'Child')
expect { belong_to(:parent).autosave }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave => true)
MESSAGE
end
end
end
context 'qualified with autosave(true)' do
context 'when the association has been configured with autosave: true' do
it 'matches' do
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: true,
)
expect { belong_to(:parent).autosave(true) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect Child to have a belongs_to association called parent
MESSAGE
end
end
context 'when the association has been configured with autosave: false' do
it 'does not match' do
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: false,
)
expect { belong_to(:parent).autosave(true) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave set to true)
MESSAGE
end
end
context 'when the association has not been configured with :autosave at all' do
it 'does not match' do
record = record_belonging_to(:parent, model_name: 'Child')
expect { belong_to(:parent).autosave(true) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave set to true)
MESSAGE
end
end
end
context 'qualified with autosave(false)' do
context 'when the association has been configured with autosave: true' do
it 'does not match' do
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: true,
)
expect { belong_to(:parent).autosave(false) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave set to false)
MESSAGE
end
end
context 'when the association has been configured with autosave: false' do
it 'matches' do
record = record_belonging_to(
:parent,
model_name: 'Child',
autosave: false,
)
expect { belong_to(:parent).autosave(false) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect Child to have a belongs_to association called parent
MESSAGE
end
end
context 'when the association has not been configured with :autosave at all' do
it 'does not match' do
record = record_belonging_to(:parent, model_name: 'Child')
expect { belong_to(:parent).autosave(false) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected Child to have a belongs_to association called parent (parent should have autosave => false)
MESSAGE
end
end
end
def record_belonging_to(
attribute_name,
model_name: 'Child',