-
-
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
Validate confirmation of with integers #1012
Changes from all commits
33a57d5
5fa474d
6973497
4953b4f
4a7b976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,28 +94,48 @@ def simple_description | |
|
||
def matches?(subject) | ||
super(subject) | ||
|
||
disallows_different_value && | ||
allows_same_value && | ||
allows_missing_confirmation | ||
type = find_type(subject) | ||
disallows_different_value(type) && | ||
allows_same_value(type) && | ||
allows_missing_confirmation(type) | ||
end | ||
|
||
private | ||
|
||
def disallows_different_value | ||
disallows_value_of('different value') do |matcher| | ||
qualify_matcher(matcher, 'some value') | ||
def find_type(subject) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few things here:
|
||
subject.class.columns_hash['attribute_to_confirm'].type rescue :string | ||
end | ||
|
||
def different_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about defining these as constants rather than methods? Also, what do you think about using the plural ( |
||
{ integer: 3, string: 'different value' } | ||
end | ||
|
||
def some_value | ||
{ integer: 2, string: 'some value' } | ||
end | ||
|
||
def same_value | ||
{ integer: 2, string: 'same value' } | ||
end | ||
|
||
def any_value | ||
{ integer: 2, string: 'any value' } | ||
end | ||
|
||
def disallows_different_value(type) | ||
disallows_value_of(different_value[type]) do |matcher| | ||
qualify_matcher(matcher, some_value[type]) | ||
end | ||
end | ||
|
||
def allows_same_value | ||
allows_value_of('same value') do |matcher| | ||
qualify_matcher(matcher, 'same value') | ||
def allows_same_value(type) | ||
allows_value_of(same_value[type]) do |matcher| | ||
qualify_matcher(matcher, same_value[type]) | ||
end | ||
end | ||
|
||
def allows_missing_confirmation | ||
allows_value_of('any value') do |matcher| | ||
def allows_missing_confirmation(type) | ||
allows_value_of(any_value[type]) do |matcher| | ||
qualify_matcher(matcher, nil) | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,4 +146,36 @@ | |
def validation_matcher_scenario_args | ||
super.deep_merge(matcher_name: :validate_confirmation_of) | ||
end | ||
|
||
context 'when attribute to confirm is type integer' do | ||
context 'when the model has a confirmation validation' do | ||
it 'passes' do | ||
builder = builder_for_record_validating_confirmation(type: :integer) | ||
expect(builder.record). | ||
to validate_confirmation_of(builder.attribute_to_confirm) | ||
end | ||
end | ||
|
||
context 'when the model has all attributes, but does not have the confirmation'do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's tweak this wording a bit. What do you think about just using:
|
||
it 'fails with an appropriate error message' do | ||
model = define_model(:example, attribute_to_confirm: :integer) do | ||
attr_accessor :attribute_to_confirm_confirmation | ||
end | ||
|
||
assertion = lambda do | ||
expect(model.new).to validate_confirmation_of(:attribute_to_confirm) | ||
end | ||
|
||
message = <<-MESSAGE | ||
Example did not properly validate that | ||
:attribute_to_confirm_confirmation matches :attribute_to_confirm. | ||
After setting :attribute_to_confirm_confirmation to ‹2›, then setting | ||
:attribute_to_confirm to ‹3›, the matcher expected the Example to be | ||
invalid, but it was valid instead. | ||
MESSAGE | ||
|
||
expect(&assertion).to fail_with_message(message) | ||
end | ||
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.
If
find_type
no longer takes asubject
, then you don't really need to assign a variable to the method -- you can use it as though it were a variable, and that means you don't need to pass it to every place that needs it. What do you think about renamingfind_type
to simplyattribute_type
?