feat: Add support for validating multiple attributes at once#1651
Open
matsales28 wants to merge 1 commit into
Open
feat: Add support for validating multiple attributes at once#1651matsales28 wants to merge 1 commit into
matsales28 wants to merge 1 commit into
Conversation
8fd0a26 to
22c7f16
Compare
655047a to
802626c
Compare
This commit adds support for asserting against multiple attributes in a
single matcher expression. Instead of writing one expectation per
attribute, you can pass the attributes as positional arguments:
```ruby
class Robot
include ActiveModel::Model
attr_accessor :arms, :legs
validates_presence_of :arms, :legs
end
RSpec.describe Robot, type: :model do
it { expect(subject).to validate_presence_of(:arms, :legs) }
end
```
This is implemented by a new `Shoulda::Matchers::MatcherCollection`
class that wraps individual matchers and produces a combined
description and per-attribute failure reasons in a single message.
The following matchers now support multiple attributes:
- `validate_presence_of`
- `validate_absence_of`
- `validate_acceptance_of`
- `validate_confirmation_of`
- `validate_length_of`
- `validate_numericality_of`
- `validate_inclusion_of`
- `validate_exclusion_of`
- `validate_comparison_of`
- `have_readonly_attribute`
- `have_one_attached`
Qualifiers chained on the matcher are applied to every wrapped
attribute uniformly. For example:
```ruby
expect(subject).to validate_presence_of(:arms, :legs).allow_nil
```
asserts that both `:arms` and `:legs` allow nil. If any attribute
fails the expectation, the failure message lists every failed
attribute and the specific reason each one failed.
802626c to
67927c2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit adds support for asserting against multiple attributes in a
single matcher expression. Instead of writing one expectation per
attribute, you can pass the attributes as positional arguments:
This is implemented by a new
Shoulda::Matchers::MatcherCollectionclass that wraps individual matchers and produces a combined
description and per-attribute failure reasons in a single message.
The following matchers now support multiple attributes:
validate_presence_ofvalidate_absence_ofvalidate_acceptance_ofvalidate_confirmation_ofvalidate_length_ofvalidate_numericality_ofvalidate_inclusion_ofvalidate_exclusion_ofvalidate_comparison_ofhave_readonly_attributehave_one_attachedQualifiers chained on the matcher are applied to every wrapped
attribute uniformly. For example:
asserts that both
:armsand:legsallow nil. If any attributefails the expectation, the failure message lists every failed
attribute and the specific reason each one failed.