mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
e708789714
Secondary author: Mauro George <maurogot@gmail.com> 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.
51 lines
1.2 KiB
Ruby
51 lines
1.2 KiB
Ruby
module UnitTests
|
|
module Matchers
|
|
extend RSpec::Matchers::DSL
|
|
|
|
matcher :fail_with_message do |expected|
|
|
def supports_block_expectations?
|
|
true
|
|
end
|
|
|
|
match do |block|
|
|
@actual = nil
|
|
|
|
begin
|
|
block.call
|
|
rescue RSpec::Expectations::ExpectationNotMetError => ex
|
|
@actual = ex.message
|
|
end
|
|
|
|
@actual && @actual == expected.sub(/\n\z/, '')
|
|
end
|
|
|
|
def failure_message
|
|
lines = ['Expectation should have failed with message:']
|
|
lines << Shoulda::Matchers::Util.indent(expected, 2)
|
|
|
|
if @actual
|
|
lines << 'Actually failed with:'
|
|
lines << Shoulda::Matchers::Util.indent(@actual, 2)
|
|
else
|
|
lines << 'However, the expectation did not fail.'
|
|
end
|
|
|
|
lines.join("\n")
|
|
end
|
|
|
|
def failure_message_for_should
|
|
failure_message
|
|
end
|
|
|
|
def failure_message_when_negated
|
|
lines = ['Expectation should not have failed with message:']
|
|
lines << Shoulda::Matchers::Util.indent(expected, 2)
|
|
lines.join("\n")
|
|
end
|
|
|
|
def failure_message_for_should_not
|
|
failure_message_when_negated
|
|
end
|
|
end
|
|
end
|
|
end
|