-
-
Notifications
You must be signed in to change notification settings - Fork 910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Ensure DefineEnumForMatcher#validating handles Hash enum values correctly #1646
fix: Ensure DefineEnumForMatcher#validating handles Hash enum values correctly #1646
Conversation
…correctly Previously, DefineEnumForMatcher#validating only worked with Array enum values. This fix ensures it now also correctly handles Hash enum values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I simply applied the test code for the existing Array to Hash as well.
If there is another better way to do this, please let me know.
shoulda-matchers/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb
Lines 909 to 1115 in d56a4b8
describe 'qualified with #validating' do | |
context 'if enum is being validated' do | |
context 'but validating qualifier is not used' do | |
it 'matches' do | |
record = build_record_with_array_values(attribute_name: :attr, default: 'published', validate: true) | |
matcher = lambda do | |
define_enum_for(:attr).with_values(['published', 'unpublished', 'draft']) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
end | |
context 'and validating qualifier is used as false' do | |
it 'rejects with an appropriate failure message' do | |
record = build_record_with_array_values(attribute_name: :attr, default: 'published', validate: true) | |
assertion = lambda do | |
expect(record). | |
to define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating(false) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›. However, :attr is being validated. | |
MESSAGE | |
expect(&assertion).to fail_with_message(message) | |
end | |
end | |
context 'and validating qualifier is used' do | |
it 'matches' do | |
record = build_record_with_array_values(attribute_name: :attr, validate: true) | |
matcher = lambda do | |
define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated not allowing nil values, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
end | |
context 'using allow_nil' do | |
context 'when allowing nil on qualifier' do | |
it 'matches' do | |
record = build_record_with_array_values(attribute_name: :attr, validate: { allow_nil: true }) | |
matcher = lambda do | |
define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating(allowing_nil: true) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated allowing nil values, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
end | |
context 'when not allowing nil on qualifier' do | |
it 'rejects with an appropriate failure message' do | |
record = build_record_with_array_values(attribute_name: :attr, validate: { allow_nil: true }) | |
assertion = lambda do | |
expect(record). | |
to define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated not allowing nil values. However, :attr is | |
allowing nil values. | |
MESSAGE | |
expect(&assertion).to fail_with_message(message) | |
end | |
end | |
end | |
end | |
context 'when not allowing nil values' do | |
it 'matches if qualifier does not allow' do | |
record = build_record_with_array_values(attribute_name: :attr, validate: { allow_nil: false }) | |
matcher = lambda do | |
define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating(allowing_nil: false) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated not allowing nil values, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
it 'rejects with an appropriate failure message if qualifier allows' do | |
record = build_record_with_array_values(attribute_name: :attr, validate: { allow_nil: false }) | |
assertion = lambda do | |
expect(record). | |
to define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating(allowing_nil: true) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated allowing nil values. However, :attr is allowing | |
nil values. | |
MESSAGE | |
expect(&assertion).to fail_with_message(message) | |
end | |
end | |
context 'if enum is not being validated' do | |
context 'but validating qualifier is used' do | |
it 'rejects with an appropriate failure message' do | |
record = build_record_with_array_values(attribute_name: :attr, default: 'published') | |
assertion = lambda do | |
expect(record). | |
to define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, and being validated not allowing nil values. However, :attr | |
is not being validated. | |
MESSAGE | |
expect(&assertion).to fail_with_message(message) | |
end | |
end | |
context 'and validating qualifier is used as false' do | |
it 'matches' do | |
record = build_record_with_array_values(attribute_name: :attr, default: 'published') | |
matcher = lambda do | |
define_enum_for(:attr). | |
with_values(['published', 'unpublished', 'draft']). | |
validating(false) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
end | |
context 'and validating qualifier is not used' do | |
it 'matches' do | |
record = build_record_with_array_values(attribute_name: :attr, default: 'published') | |
matcher = lambda do | |
define_enum_for(:attr).with_values(['published', 'unpublished', 'draft']) | |
end | |
message = format_message(<<-MESSAGE) | |
Expected Example not to define :attr as an enum backed by an integer, | |
mapping ‹"published"› to ‹0›, ‹"unpublished"› to ‹1›, and ‹"draft"› | |
to ‹2›, but it did. | |
MESSAGE | |
expect(&matcher).to match_against(record).or_fail_with(message) | |
end | |
end | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for fixing that!
Why
DefineEnumForMatcher#validating
is added at #1630At 6.3.0,
DefineEnumForMatcher#validating
only worked with Array enum values. This PR ensures it now also correctly handles Hash enum values.How it works
At 6.3.0
assertion fails. This is because the existing code only assumes that enum values is an Array.
By this patch
assertion succeed.