Skip to content
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

Add more tests to the validate_numericality_of error message #699

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/active_model/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def has_messages?
end

def messages_description
if has_messages?
if has_messages? && !record.valid?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this change was to specifically fix maurogeorge@84b7175#diff-d7c315235a16eb29151b8251207fbd67R539, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still seems odd to me, I'm investigating why we have to do this. Seems odd that the record has error messages but then it's valid at the same time.

" errors:\n#{pretty_error_messages(record)}"
else
' no errors'
Expand Down
2 changes: 1 addition & 1 deletion spec/support/unit/matchers/fail_with_message_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def supports_block_expectations?
@actual = ex.message
end

@actual && @actual == expected
@actual && @actual == expected.sub(/\n\z/, '')
end

def failure_message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,51 +505,93 @@ def default_validation_values
even: true,
greater_than: 18
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the operands of an expression in an assignment spanning multiple lines.

only_integer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the operands of an expression in an assignment spanning multiple lines.

is_greater_than(18)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the operands of an expression in an assignment spanning multiple lines.

end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be an integer" when attr is set to 0.1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

got errors:
* "must be even" (attribute: attr, value: "19")
MESSAGE
)
end

it do
record = build_record_validating_numericality(greater_than: 18)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be an integer" when attr is set to 0.1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

got no errors
MESSAGE
)
end

it do
record = build_record_validating_numericality(
even: true,
greater_than_or_equal_to: 18
)
expect(record).
not_to validate_numericality.
even.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
even.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got no errors
MESSAGE
)
end

it do
record = build_record_validating_numericality(
odd: true,
greater_than: 18
)
expect(record).
not_to validate_numericality.
even.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
even.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be even" when attr is set to 1,
got errors:
* "must be odd" (attribute: attr, value: "20")
MESSAGE
)
end

it do
record = build_record_validating_numericality(
odd: true,
greater_than_or_equal_to: 99
)
expect(record).
not_to validate_numericality.
odd.
is_less_than_or_equal_to(99)
assertion = lambda do
expect(record).
to validate_numericality.
odd.
is_less_than_or_equal_to(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be less than or equal to 99" when attr is set to 101,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [96/80]

got no errors
MESSAGE
)
end

it do
Expand All @@ -558,11 +600,19 @@ def default_validation_values
greater_than_or_equal_to: 18,
less_than: 99
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got no errors
MESSAGE
)
end
end

Expand All @@ -572,65 +622,116 @@ def default_validation_values
only_integer: true,
greater_than: 19
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got errors:
* "must be greater than 19" (attribute: attr, value: "19")
MESSAGE
)
end

it do
record = build_record_validating_numericality(
only_integer: true,
greater_than: 17
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got no errors
MESSAGE
)
end

it do
record = build_record_validating_numericality(
even: true,
greater_than: 20
)
expect(record).
not_to validate_numericality.
even.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
even.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got errors:
* "must be greater than 20" (attribute: attr, value: "20")
MESSAGE
)
end

it do
record = build_record_validating_numericality(
even: true,
greater_than: 16
)
expect(record).
not_to validate_numericality.
even.
is_greater_than(18)
assertion = lambda do
expect(record).
to validate_numericality.
even.
is_greater_than(18)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

got no errors
MESSAGE
)
end

it do
record = build_record_validating_numericality(
odd: true,
less_than_or_equal_to: 101
)
expect(record).
not_to validate_numericality.
odd.
is_less_than_or_equal_to(99)
assertion = lambda do
expect(record).
to validate_numericality.
odd.
is_less_than_or_equal_to(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be less than or equal to 99" when attr is set to 101,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [96/80]

got no errors
MESSAGE
)
end

it do
record = build_record_validating_numericality(
odd: true,
less_than_or_equal_to: 97
)
expect(record).
not_to validate_numericality.
odd.
is_less_than_or_equal_to(99)
assertion = lambda do
expect(record).
to validate_numericality.
odd.
is_less_than_or_equal_to(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be less than or equal to 99" when attr is set to 101,
got errors:
* "must be less than or equal to 97" (attribute: attr, value: "101")
MESSAGE
)
end

it do
Expand All @@ -639,11 +740,20 @@ def default_validation_values
greater_than: 19,
less_than: 99
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be greater than 18" when attr is set to 18,
got errors:
* "must be less than 99" (attribute: attr, value: "100")
MESSAGE
)
end

it do
Expand All @@ -652,11 +762,20 @@ def default_validation_values
greater_than: 18,
less_than: 100
)
expect(record).
not_to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
assertion = lambda do
expect(record).
to validate_numericality.
only_integer.
is_greater_than(18).
is_less_than(99)
end
expect(&assertion).to fail_with_message(
<<-MESSAGE.strip_heredoc
Expected errors to include "must be less than 99" when attr is set to 100,
got errors:
* "must be less than 100" (attribute: attr, value: "100")
MESSAGE
)
end
end
end
Expand Down