-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
fail_with_message_matcher.rb
63 lines (50 loc) · 1.47 KB
/
fail_with_message_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module UnitTests
module Matchers
extend RSpec::Matchers::DSL
matcher :fail_with_message do |raw_expected, wrap: false|
expected =
if wrap
Shoulda::Matchers.word_wrap(raw_expected)
else
raw_expected
end
def supports_block_expectations?
true
end
match do |block|
@actual = nil
begin
block.call
rescue RSpec::Expectations::ExpectationNotMetError => e
@actual = e.message
end
@actual && @actual == expected.sub(/\n\z/, '')
end
define_method :failure_message do
lines = ['Expectation should have failed with message:']
lines << Shoulda::Matchers::Util.indent(expected, 2)
if @actual
diff = differ.diff(@actual, expected)[1..]
lines << 'Actually failed with:'
lines << Shoulda::Matchers::Util.indent(@actual, 2)
if diff
lines << 'Diff:'
lines << Shoulda::Matchers::Util.indent(diff, 2)
end
else
lines << 'However, the expectation did not fail at all.'
end
lines.join("\n")
end
define_method :failure_message_when_negated do
lines = ['Expectation should not have failed with message:']
lines << Shoulda::Matchers::Util.indent(expected, 2)
lines.join("\n")
end
private
def differ
@_differ ||= RSpec::Support::Differ.new
end
end
end
end