spec_helper.rb
1.27 KB
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
64
65
66
67
68
69
70
71
require 'rspec'
require 'view_helpers/view_example_group'
begin
require 'ruby-debug'
rescue LoadError
# no debugger available
end
RSpec.configure do |config|
config.include Module.new {
protected
def include_phrase(string)
PhraseMatcher.new(string)
end
def have_deprecation(msg)
DeprecationMatcher.new(msg)
end
}
config.mock_with :mocha
end
class PhraseMatcher
def initialize(string)
@string = string
@pattern = /\b#{Regexp.escape string}\b/
end
def matches?(actual)
@actual = actual.to_s
@actual =~ @pattern
end
def failure_message
"expected #{@actual.inspect} to contain phrase #{@string.inspect}"
end
def negative_failure_message
"expected #{@actual.inspect} not to contain phrase #{@string.inspect}"
end
end
require 'stringio'
class DeprecationMatcher
def initialize(message)
@message = message
end
def matches?(block)
@actual = hijack_stderr(&block)
PhraseMatcher.new("DEPRECATION WARNING: #{@message}").matches?(@actual)
end
def failure_message
"expected deprecation warning #{@message.inspect}, got #{@actual.inspect}"
end
private
def hijack_stderr
err = $stderr
$stderr = StringIO.new
yield
$stderr.string.rstrip
ensure
$stderr = err
end
end