/
spec_helper.rb
121 lines (97 loc) · 2.62 KB
/
spec_helper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'rspec/support/spec'
require 'rspec/mocks/ruby_features'
RSpec::Support::Spec.setup_simplecov do
minimum_coverage 96
end
require 'yaml'
begin
require 'psych'
rescue LoadError
end
RSpec::Matchers.define :include_method do |expected|
match do |actual|
actual.map { |m| m.to_s }.include?(expected.to_s)
end
end
module VerifyAndResetHelpers
def verify(object)
proxy = RSpec::Mocks.space.proxy_for(object)
proxy.verify
ensure
proxy.reset # so it doesn't fail the verify after the example completes
end
def reset(object)
RSpec::Mocks.space.proxy_for(object).reset
end
def verify_all
RSpec::Mocks.space.verify_all
ensure
reset_all
end
def reset_all
RSpec::Mocks.space.reset_all
end
end
module VerificationHelpers
def prevents(&block)
expect(&block).to \
raise_error(RSpec::Mocks::MockExpectationError)
end
end
require 'rspec/support/spec'
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
config.order = :random
config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
# TODO: switch most things to the `expect` syntax (and configure only that here)
# but keep a small number of specs that use the old syntax in order to test it
# (using the "with syntax" shared context defined below).
config.mock_with :rspec do |mocks|
mocks.syntax = [:should, :expect]
end
old_verbose = nil
config.before(:each, :silence_warnings) do
old_verbose = $VERBOSE
$VERBOSE = nil
end
config.after(:each, :silence_warnings) do
$VERBOSE = old_verbose
end
config.include VerifyAndResetHelpers
config.include VerificationHelpers
config.extend RSpec::Mocks::RubyFeatures
config.include RSpec::Mocks::RubyFeatures
end
shared_context "with syntax" do |syntax|
orig_syntax = nil
before(:all) do
orig_syntax = RSpec::Mocks.configuration.syntax
RSpec::Mocks.configuration.syntax = syntax
end
after(:all) do
RSpec::Mocks.configuration.syntax = orig_syntax
end
end
shared_context "with isolated configuration" do
orig_configuration = nil
before do
orig_configuration = RSpec::Mocks.configuration
RSpec::Mocks.instance_variable_set(:@configuration, RSpec::Mocks::Configuration.new)
end
after do
RSpec::Mocks.instance_variable_set(:@configuration, orig_configuration)
end
end
shared_context "with the default mocks syntax" do
orig_syntax = nil
before(:all) do
orig_syntax = RSpec::Mocks.configuration.syntax
RSpec::Mocks.configuration.reset_syntaxes_to_default
end
after(:all) do
RSpec::Mocks.configuration.syntax = orig_syntax
end
end