Skip to content

Commit

Permalink
Fix failure message for numericality matcher
Browse files Browse the repository at this point in the history
Secondary author: Mauro George <[email protected]>

Sometimes the failure message did not always provide the reason for
failure. For instance, given this validation:

    validates :ano, numericality: { only_integer: true, greater_than_or_equal_to: 1900 }

this test:

    it { should validate_numericality_of(:ano).is_greater_than_or_equal_to(1900) }

would fail with the following:

    Did not expect errors  when ano is set to 1900.000000000001, got error:

as you can see, the failure message doesn't contain the validation
error.

In the process of making this fix, we changed how the matcher fails so
that it will so when the first submatcher fails, not the last. This
changes what the failure message looks like, but not the basic
functionality of the matcher itself.
  • Loading branch information
mcmire committed Jun 1, 2015
1 parent 132ab72 commit e708789
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 92 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
* Fix `validate_uniqueness_of` so that it allows you to test against scoped
attributes that are boolean columns. ([#457], [#694])

* Fix failure message for `validate_numericality_of` as it sometimes didn't
provide the reason for failure. ([#699])
### Features
* Add `on` qualifier to `permit`. This allows you to make an assertion that
Expand Down Expand Up @@ -196,6 +199,7 @@
[#457]: https://github.com/thoughtbot/shoulda-matchers/pull/457
[#694]: https://github.com/thoughtbot/shoulda-matchers/pull/694
[#692]: https://github.com/thoughtbot/shoulda-matchers/pull/692
[#699]: https://github.com/thoughtbot/shoulda-matchers/pull/699
# 2.8.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def on(context)

def matches?(subject)
@subject = subject
failing_submatchers.empty?
first_failing_submatcher.nil?
end

def description
Expand All @@ -410,12 +410,12 @@ def description
end

def failure_message
last_failing_submatcher.failure_message
first_failing_submatcher.failure_message
end
alias failure_message_for_should failure_message

def failure_message_when_negated
last_failing_submatcher.failure_message_when_negated
first_failing_submatcher.failure_message_when_negated
end
alias failure_message_for_should_not failure_message_when_negated

Expand Down Expand Up @@ -450,21 +450,10 @@ def update_diff_to_compare(matcher)
@diff_to_compare = [@diff_to_compare, matcher.diff_to_compare].max
end

def submatchers_and_results
@_submatchers_and_results ||=
@submatchers.map do |matcher|
{ matcher: matcher, matched: matcher.matches?(@subject) }
end
end

def failing_submatchers
submatchers_and_results.
select { |x| !x[:matched] }.
map { |x| x[:matcher] }
end

def last_failing_submatcher
failing_submatchers.last
def first_failing_submatcher
@_first_failing_submatcher ||= @submatchers.detect do |submatcher|
!submatcher.matches?(@subject)
end
end

def allowed_types
Expand Down
4 changes: 4 additions & 0 deletions spec/support/unit/helpers/rails_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ def rails_4_x?
def rails_gte_4_1?
rails_version >= 4.1
end

def rails_gte_4_2?
rails_version >= 4.2
end
end
end
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
Loading

0 comments on commit e708789

Please sign in to comment.