rspec-expectations 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -2
- data/.yardopts +0 -1
- data/Changelog.md +115 -35
- data/README.md +2 -2
- data/lib/rspec/expectations.rb +13 -8
- data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
- data/lib/rspec/expectations/expectation_target.rb +72 -8
- data/lib/rspec/expectations/fail_with.rb +10 -52
- data/lib/rspec/expectations/handler.rb +9 -11
- data/lib/rspec/expectations/syntax.rb +37 -35
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +60 -9
- data/lib/rspec/matchers/aliased_matcher.rb +6 -0
- data/lib/rspec/matchers/built_in.rb +9 -1
- data/lib/rspec/matchers/built_in/all.rb +78 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
- data/lib/rspec/matchers/built_in/be.rb +117 -42
- data/lib/rspec/matchers/built_in/be_between.rb +22 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
- data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
- data/lib/rspec/matchers/built_in/be_within.rb +26 -6
- data/lib/rspec/matchers/built_in/change.rb +89 -13
- data/lib/rspec/matchers/built_in/compound.rb +19 -3
- data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +20 -5
- data/lib/rspec/matchers/built_in/eql.rb +15 -3
- data/lib/rspec/matchers/built_in/equal.rb +23 -6
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +58 -3
- data/lib/rspec/matchers/built_in/include.rb +16 -1
- data/lib/rspec/matchers/built_in/match.rb +14 -4
- data/lib/rspec/matchers/built_in/operators.rb +16 -0
- data/lib/rspec/matchers/built_in/output.rb +47 -5
- data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
- data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
- data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
- data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
- data/lib/rspec/matchers/built_in/yield.rb +148 -44
- data/lib/rspec/matchers/composable.rb +48 -7
- data/lib/rspec/matchers/dsl.rb +45 -17
- data/lib/rspec/matchers/generated_descriptions.rb +7 -0
- data/lib/rspec/matchers/matcher_delegator.rb +6 -2
- data/lib/rspec/matchers/pretty.rb +15 -19
- metadata +33 -236
- metadata.gz.sig +0 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -96
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/comparisons.feature +0 -97
- data/features/built_in_matchers/contain_exactly.feature +0 -46
- data/features/built_in_matchers/cover.feature +0 -47
- data/features/built_in_matchers/end_with.feature +0 -48
- data/features/built_in_matchers/equality.feature +0 -136
- data/features/built_in_matchers/exist.feature +0 -45
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -144
- data/features/built_in_matchers/include.feature +0 -126
- data/features/built_in_matchers/match.feature +0 -51
- data/features/built_in_matchers/output.feature +0 -70
- data/features/built_in_matchers/predicates.feature +0 -161
- data/features/built_in_matchers/respond_to.feature +0 -84
- data/features/built_in_matchers/satisfy.feature +0 -33
- data/features/built_in_matchers/start_with.feature +0 -48
- data/features/built_in_matchers/throw_symbol.feature +0 -91
- data/features/built_in_matchers/types.feature +0 -116
- data/features/built_in_matchers/yield.feature +0 -161
- data/features/composing_matchers.feature +0 -250
- data/features/compound_expectations.feature +0 -45
- data/features/custom_matchers/access_running_example.feature +0 -53
- data/features/custom_matchers/define_diffable_matcher.feature +0 -27
- data/features/custom_matchers/define_matcher.feature +0 -340
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -39
- data/features/diffing.feature +0 -85
- data/features/implicit_docstrings.feature +0 -52
- data/features/step_definitions/additional_cli_steps.rb +0 -22
- data/features/support/env.rb +0 -21
- data/features/support/rubinius.rb +0 -6
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/minitest.feature +0 -44
- data/lib/rspec-expectations.rb +0 -1
- data/lib/rspec/expectations/diff_presenter.rb +0 -141
- data/lib/rspec/expectations/differ.rb +0 -44
- data/lib/rspec/expectations/encoded_string.rb +0 -56
- data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
- data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -114
- data/spec/rspec/expectations/handler_spec.rb +0 -205
- data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
- data/spec/rspec/expectations/syntax_spec.rb +0 -89
- data/spec/rspec/expectations_spec.rb +0 -12
- data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
- data/spec/rspec/matchers/aliases_spec.rb +0 -449
- data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
- data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
- data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
- data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
- data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
- data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
- data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
- data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
- data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
- data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
- data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
- data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
- data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
- data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
- data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
- data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
- data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
- data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
- data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
- data/spec/rspec/matchers/configuration_spec.rb +0 -213
- data/spec/rspec/matchers/description_generation_spec.rb +0 -191
- data/spec/rspec/matchers/dsl_spec.rb +0 -895
- data/spec/rspec/matchers/legacy_spec.rb +0 -101
- data/spec/rspec/matchers_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -85
- data/spec/support/matchers.rb +0 -22
- data/spec/support/shared_examples.rb +0 -35
@@ -1,461 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "expect { ... }.to raise_error" do
|
4
|
-
it_behaves_like("an RSpec matcher", :valid_value => lambda { raise "boom" },
|
5
|
-
:invalid_value => lambda { }) do
|
6
|
-
let(:matcher) { raise_error(/boom/) }
|
7
|
-
end
|
8
|
-
|
9
|
-
it "passes if anything is raised" do
|
10
|
-
expect {raise}.to raise_error
|
11
|
-
end
|
12
|
-
|
13
|
-
it "passes if an error instance is expected" do
|
14
|
-
s = StandardError.new
|
15
|
-
expect {raise s}.to raise_error(s)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "fails if a different error instance is thrown from the one that is expected" do
|
19
|
-
s = StandardError.new("Error 1")
|
20
|
-
to_raise = StandardError.new("Error 2")
|
21
|
-
expect do
|
22
|
-
expect {raise to_raise}.to raise_error(s)
|
23
|
-
end.to fail_with(Regexp.new("expected #{s.inspect}, got #{to_raise.inspect} with backtrace"))
|
24
|
-
end
|
25
|
-
|
26
|
-
it "passes if an error class is expected and an instance of that class is thrown" do
|
27
|
-
s = StandardError.new :bees
|
28
|
-
|
29
|
-
expect { raise s }.to raise_error(StandardError)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "fails if nothing is raised" do
|
33
|
-
expect {
|
34
|
-
expect {}.to raise_error
|
35
|
-
}.to fail_with("expected Exception but nothing was raised")
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "raise_exception aliased to raise_error" do
|
40
|
-
it "passes if anything is raised" do
|
41
|
-
expect {raise}.to raise_exception
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe "expect { ... }.to raise_error {|err| ... }" do
|
46
|
-
it "passes if there is an error" do
|
47
|
-
ran = false
|
48
|
-
expect { non_existent_method }.to raise_error {|e|
|
49
|
-
ran = true
|
50
|
-
}
|
51
|
-
expect(ran).to be_truthy
|
52
|
-
end
|
53
|
-
|
54
|
-
it "passes the error to the block" do
|
55
|
-
error = nil
|
56
|
-
expect { non_existent_method }.to raise_error {|e|
|
57
|
-
error = e
|
58
|
-
}
|
59
|
-
expect(error).to be_kind_of(NameError)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "expect { ... }.to raise_error do |err| ... end" do
|
64
|
-
it "passes the error to the block" do
|
65
|
-
error = nil
|
66
|
-
expect { non_existent_method }.to raise_error do |e|
|
67
|
-
error = e
|
68
|
-
end
|
69
|
-
expect(error).to be_kind_of(NameError)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "expect { ... }.to(raise_error { |err| ... }) do |err| ... end" do
|
74
|
-
it "passes the error only to the block taken directly by #raise_error" do
|
75
|
-
error_passed_to_curly = nil
|
76
|
-
error_passed_to_do_end = nil
|
77
|
-
|
78
|
-
expect { non_existent_method }.to(raise_error { |e| error_passed_to_curly = e }) do |e|
|
79
|
-
error_passed_to_do_end = e
|
80
|
-
end
|
81
|
-
|
82
|
-
expect(error_passed_to_curly).to be_kind_of(NameError)
|
83
|
-
expect(error_passed_to_do_end).to be_nil
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
describe "expect { ... }.not_to raise_error" do
|
88
|
-
|
89
|
-
context "with a specific error class" do
|
90
|
-
it "is invalid" do
|
91
|
-
expect {
|
92
|
-
expect {"bees"}.not_to raise_error(RuntimeError)
|
93
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(SpecificErrorClass\)` is not valid/)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context "with no specific error class" do
|
98
|
-
it "passes if nothing is raised" do
|
99
|
-
expect {}.not_to raise_error
|
100
|
-
end
|
101
|
-
|
102
|
-
it "fails if anything is raised" do
|
103
|
-
expect {
|
104
|
-
expect { raise RuntimeError, "example message" }.not_to raise_error
|
105
|
-
}.to fail_with(/expected no Exception, got #<RuntimeError: example message>/)
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'includes the backtrace of the error that was raised in the error message' do
|
109
|
-
expect {
|
110
|
-
expect { raise "boom" }.not_to raise_error
|
111
|
-
}.to raise_error { |e|
|
112
|
-
backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
|
113
|
-
expect(e.message).to include("with backtrace", backtrace_line)
|
114
|
-
}
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'formats the backtrace using the configured backtrace formatter' do
|
118
|
-
allow(RSpec::Matchers.configuration.backtrace_formatter).
|
119
|
-
to receive(:format_backtrace).
|
120
|
-
and_return("formatted-backtrace")
|
121
|
-
|
122
|
-
expect {
|
123
|
-
expect { raise "boom" }.not_to raise_error
|
124
|
-
}.to raise_error { |e|
|
125
|
-
expect(e.message).to include("with backtrace", "formatted-backtrace")
|
126
|
-
}
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
describe "expect { ... }.to raise_error(message)" do
|
132
|
-
it "passes if RuntimeError is raised with the right message" do
|
133
|
-
expect {raise 'blah'}.to raise_error('blah')
|
134
|
-
end
|
135
|
-
|
136
|
-
it "passes if RuntimeError is raised with a matching message" do
|
137
|
-
expect {raise 'blah'}.to raise_error(/blah/)
|
138
|
-
end
|
139
|
-
|
140
|
-
it "passes if any other error is raised with the right message" do
|
141
|
-
expect {raise NameError.new('blah')}.to raise_error('blah')
|
142
|
-
end
|
143
|
-
|
144
|
-
it "fails if RuntimeError error is raised with the wrong message" do
|
145
|
-
expect do
|
146
|
-
expect {raise 'blarg'}.to raise_error('blah')
|
147
|
-
end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
|
148
|
-
end
|
149
|
-
|
150
|
-
it "fails if any other error is raised with the wrong message" do
|
151
|
-
expect do
|
152
|
-
expect {raise NameError.new('blarg')}.to raise_error('blah')
|
153
|
-
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
|
154
|
-
end
|
155
|
-
|
156
|
-
it 'includes the backtrace of any other error in the failure message' do
|
157
|
-
expect {
|
158
|
-
expect { raise "boom" }.to raise_error(ArgumentError)
|
159
|
-
}.to raise_error { |e|
|
160
|
-
backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
|
161
|
-
expect(e.message).to include("with backtrace", backtrace_line)
|
162
|
-
}
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
describe "expect { ... }.to raise_error.with_message(message)" do
|
167
|
-
it "raises an argument error if raise_error itself expects a message" do
|
168
|
-
expect {
|
169
|
-
expect { }.to raise_error("bees").with_message("sup")
|
170
|
-
}.to raise_error.with_message(/`expect \{ \}\.to raise_error\(message\)\.with_message\(message\)` is not valid/)
|
171
|
-
end
|
172
|
-
|
173
|
-
it "passes if RuntimeError is raised with the right message" do
|
174
|
-
expect {raise 'blah'}.to raise_error.with_message('blah')
|
175
|
-
end
|
176
|
-
|
177
|
-
it "passes if RuntimeError is raised with a matching message" do
|
178
|
-
expect {raise 'blah'}.to raise_error.with_message(/blah/)
|
179
|
-
end
|
180
|
-
|
181
|
-
it "passes if any other error is raised with the right message" do
|
182
|
-
expect {raise NameError.new('blah')}.to raise_error.with_message('blah')
|
183
|
-
end
|
184
|
-
|
185
|
-
it "fails if RuntimeError error is raised with the wrong message" do
|
186
|
-
expect do
|
187
|
-
expect {raise 'blarg'}.to raise_error.with_message('blah')
|
188
|
-
end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
|
189
|
-
end
|
190
|
-
|
191
|
-
it "fails if any other error is raised with the wrong message" do
|
192
|
-
expect do
|
193
|
-
expect {raise NameError.new('blarg')}.to raise_error.with_message('blah')
|
194
|
-
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
describe "expect { ... }.not_to raise_error(message)" do
|
199
|
-
it "is invalid" do
|
200
|
-
expect {
|
201
|
-
expect {raise 'blarg'}.not_to raise_error(/blah/)
|
202
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(message\)` is not valid/)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
describe "expect { ... }.to raise_error(NamedError)" do
|
207
|
-
it "passes if named error is raised" do
|
208
|
-
expect { non_existent_method }.to raise_error(NameError)
|
209
|
-
end
|
210
|
-
|
211
|
-
it "fails if nothing is raised" do
|
212
|
-
expect {
|
213
|
-
expect { }.to raise_error(NameError)
|
214
|
-
}.to fail_with(/expected NameError but nothing was raised/)
|
215
|
-
end
|
216
|
-
|
217
|
-
it "fails if another error is raised (NameError)" do
|
218
|
-
expect {
|
219
|
-
expect { raise RuntimeError, "example message" }.to raise_error(NameError)
|
220
|
-
}.to fail_with(/expected NameError, got #<RuntimeError: example message>/)
|
221
|
-
end
|
222
|
-
|
223
|
-
it "fails if another error is raised (NameError)" do
|
224
|
-
expect {
|
225
|
-
expect { load "non/existent/file" }.to raise_error(NameError)
|
226
|
-
}.to fail_with(/expected NameError, got #<LoadError/)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
describe "expect { ... }.not_to raise_error(NamedError)" do
|
231
|
-
it "is invalid" do
|
232
|
-
expect {
|
233
|
-
expect { }.not_to raise_error(NameError)
|
234
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(SpecificErrorClass\)` is not valid/)
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
describe "expect { ... }.to raise_error(NamedError, error_message) with String" do
|
239
|
-
it "passes if named error is raised with same message" do
|
240
|
-
expect { raise "example message" }.to raise_error(RuntimeError, "example message")
|
241
|
-
end
|
242
|
-
|
243
|
-
it "fails if nothing is raised" do
|
244
|
-
expect {
|
245
|
-
expect {}.to raise_error(RuntimeError, "example message")
|
246
|
-
}.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
|
247
|
-
end
|
248
|
-
|
249
|
-
it "fails if incorrect error is raised" do
|
250
|
-
expect {
|
251
|
-
expect { raise RuntimeError, "example message" }.to raise_error(NameError, "example message")
|
252
|
-
}.to fail_with(/expected NameError with \"example message\", got #<RuntimeError: example message>/)
|
253
|
-
end
|
254
|
-
|
255
|
-
it "fails if correct error is raised with incorrect message" do
|
256
|
-
expect {
|
257
|
-
expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, "example message")
|
258
|
-
}.to fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
describe "expect { ... }.not_to raise_error(NamedError, error_message) with String" do
|
263
|
-
it "is invalid" do
|
264
|
-
expect {
|
265
|
-
expect {}.not_to raise_error(RuntimeError, "example message")
|
266
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(SpecificErrorClass, message\)` is not valid/)
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
describe "expect { ... }.to raise_error(NamedError, error_message) with Regexp" do
|
271
|
-
it "passes if named error is raised with matching message" do
|
272
|
-
expect { raise "example message" }.to raise_error(RuntimeError, /ample mess/)
|
273
|
-
end
|
274
|
-
|
275
|
-
it "fails if nothing is raised" do
|
276
|
-
expect {
|
277
|
-
expect {}.to raise_error(RuntimeError, /ample mess/)
|
278
|
-
}.to fail_with(/expected RuntimeError with message matching \/ample mess\/ but nothing was raised/)
|
279
|
-
end
|
280
|
-
|
281
|
-
it "fails if incorrect error is raised" do
|
282
|
-
expect {
|
283
|
-
expect { raise RuntimeError, "example message" }.to raise_error(NameError, /ample mess/)
|
284
|
-
}.to fail_with(/expected NameError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
|
285
|
-
end
|
286
|
-
|
287
|
-
it "fails if correct error is raised with incorrect message" do
|
288
|
-
expect {
|
289
|
-
expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, /less than ample mess/)
|
290
|
-
}.to fail_with(/expected RuntimeError with message matching \/less than ample mess\/, got #<RuntimeError: not the example message>/)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe "expect { ... }.not_to raise_error(NamedError, error_message) with Regexp" do
|
295
|
-
it "is invalid" do
|
296
|
-
expect {
|
297
|
-
expect {}.not_to raise_error(RuntimeError, /ample mess/)
|
298
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(SpecificErrorClass, message\)` is not valid/)
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }" do
|
303
|
-
it "yields exception if named error is raised with same message" do
|
304
|
-
ran = false
|
305
|
-
|
306
|
-
expect {
|
307
|
-
raise "example message"
|
308
|
-
}.to raise_error(RuntimeError, "example message") { |err|
|
309
|
-
ran = true
|
310
|
-
expect(err.class).to eq RuntimeError
|
311
|
-
expect(err.message).to eq "example message"
|
312
|
-
}
|
313
|
-
|
314
|
-
expect(ran).to be(true)
|
315
|
-
end
|
316
|
-
|
317
|
-
it "yielded block fails on it's own right" do
|
318
|
-
ran, passed = false, false
|
319
|
-
|
320
|
-
expect {
|
321
|
-
expect {
|
322
|
-
raise "example message"
|
323
|
-
}.to raise_error(RuntimeError, "example message") { |err|
|
324
|
-
ran = true
|
325
|
-
expect(5).to eq 4
|
326
|
-
passed = true
|
327
|
-
}
|
328
|
-
}.to fail_with(/expected: 4/m)
|
329
|
-
|
330
|
-
expect(ran).to be_truthy
|
331
|
-
expect(passed).to be_falsey
|
332
|
-
end
|
333
|
-
|
334
|
-
it "does NOT yield exception if no error was thrown" do
|
335
|
-
ran = false
|
336
|
-
|
337
|
-
expect {
|
338
|
-
expect {}.to raise_error(RuntimeError, "example message") { |err|
|
339
|
-
ran = true
|
340
|
-
}
|
341
|
-
}.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
|
342
|
-
|
343
|
-
expect(ran).to eq false
|
344
|
-
end
|
345
|
-
|
346
|
-
it "does not yield exception if error class is not matched" do
|
347
|
-
ran = false
|
348
|
-
|
349
|
-
expect {
|
350
|
-
expect {
|
351
|
-
raise "example message"
|
352
|
-
}.to raise_error(SyntaxError, "example message") { |err|
|
353
|
-
ran = true
|
354
|
-
}
|
355
|
-
}.to fail_with(/expected SyntaxError with \"example message\", got #<RuntimeError: example message>/)
|
356
|
-
|
357
|
-
expect(ran).to eq false
|
358
|
-
end
|
359
|
-
|
360
|
-
it "does NOT yield exception if error message is not matched" do
|
361
|
-
ran = false
|
362
|
-
|
363
|
-
expect {
|
364
|
-
expect {
|
365
|
-
raise "example message"
|
366
|
-
}.to raise_error(RuntimeError, "different message") { |err|
|
367
|
-
ran = true
|
368
|
-
}
|
369
|
-
}.to fail_with(/expected RuntimeError with \"different message\", got #<RuntimeError: example message>/)
|
370
|
-
|
371
|
-
expect(ran).to eq false
|
372
|
-
end
|
373
|
-
end
|
374
|
-
|
375
|
-
describe "expect { ... }.not_to raise_error(NamedError, error_message) { |err| ... }" do
|
376
|
-
it "is invalid" do
|
377
|
-
expect {
|
378
|
-
expect {}.not_to raise_error(RuntimeError, "example message") { |err| }
|
379
|
-
}.to raise_error(/`expect \{ \}\.not_to raise_error\(SpecificErrorClass, message\)` is not valid/)
|
380
|
-
end
|
381
|
-
end
|
382
|
-
|
383
|
-
describe "misuse of raise_error, with (), not {}" do
|
384
|
-
it "fails with warning" do
|
385
|
-
expect(::Kernel).to receive(:warn).with(/`raise_error` was called with non-proc object 1\.7/)
|
386
|
-
expect {
|
387
|
-
expect(Math.sqrt(3)).to raise_error
|
388
|
-
}.to fail_with(/nothing was raised/)
|
389
|
-
end
|
390
|
-
end
|
391
|
-
|
392
|
-
describe "Composing matchers with `raise_error`" do
|
393
|
-
matcher :an_error_with_attribute do |attr|
|
394
|
-
chain :equal_to do |value|
|
395
|
-
@expected_value = value
|
396
|
-
end
|
397
|
-
|
398
|
-
match do |error|
|
399
|
-
error.__send__(attr) == @expected_value
|
400
|
-
end
|
401
|
-
|
402
|
-
description do
|
403
|
-
super() + " equal to #{@expected_value}"
|
404
|
-
end
|
405
|
-
end
|
406
|
-
|
407
|
-
class FooError < StandardError
|
408
|
-
def foo; :bar; end
|
409
|
-
end
|
410
|
-
|
411
|
-
describe "expect { }.to raise_error(matcher)" do
|
412
|
-
it 'passes when the matcher matches the raised error' do
|
413
|
-
expect { raise FooError }.to raise_error(an_error_with_attribute(:foo).equal_to(:bar))
|
414
|
-
end
|
415
|
-
|
416
|
-
it 'fails with a clear message when the matcher does not match the raised error' do
|
417
|
-
expect {
|
418
|
-
expect { raise FooError }.to raise_error(an_error_with_attribute(:foo).equal_to(3))
|
419
|
-
}.to fail_matching("expected an error with attribute :foo equal to 3, got #<FooError: FooError>")
|
420
|
-
end
|
421
|
-
|
422
|
-
it 'provides a description' do
|
423
|
-
description = raise_error(an_error_with_attribute(:foo).equal_to(3)).description
|
424
|
-
expect(description).to eq("raise an error with attribute :foo equal to 3")
|
425
|
-
end
|
426
|
-
end
|
427
|
-
|
428
|
-
describe "expect { }.to raise_error(ErrorClass, matcher)" do
|
429
|
-
it 'passes when the class and matcher match the raised error' do
|
430
|
-
expect { raise FooError, "food" }.to raise_error(FooError, a_string_including("foo"))
|
431
|
-
end
|
432
|
-
|
433
|
-
it 'fails with a clear message when the matcher does not match the raised error' do
|
434
|
-
expect {
|
435
|
-
expect { raise FooError, "food" }.to raise_error(FooError, a_string_including("bar"))
|
436
|
-
}.to fail_matching('expected FooError with a string including "bar", got #<FooError: food')
|
437
|
-
end
|
438
|
-
|
439
|
-
it 'provides a description' do
|
440
|
-
description = raise_error(FooError, a_string_including("foo")).description
|
441
|
-
expect(description).to eq('raise FooError with a string including "foo"')
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
describe "expect { }.to raise_error(ErrorClass).with_message(matcher)" do
|
446
|
-
it 'passes when the class and matcher match the raised error' do
|
447
|
-
expect { raise FooError, "food" }.to raise_error(FooError).with_message(a_string_including("foo"))
|
448
|
-
end
|
449
|
-
|
450
|
-
it 'fails with a clear message when the matcher does not match the raised error' do
|
451
|
-
expect {
|
452
|
-
expect { raise FooError, "food" }.to raise_error(FooError).with_message(a_string_including("bar"))
|
453
|
-
}.to fail_matching('expected FooError with a string including "bar", got #<FooError: food')
|
454
|
-
end
|
455
|
-
|
456
|
-
it 'provides a description' do
|
457
|
-
description = raise_error(FooError).with_message(a_string_including("foo")).description
|
458
|
-
expect(description).to eq('raise FooError with a string including "foo"')
|
459
|
-
end
|
460
|
-
end
|
461
|
-
end
|