rspec-expectations 2.13.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. checksums.yaml +15 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/Changelog.md +413 -23
  4. data/License.txt +1 -0
  5. data/README.md +109 -80
  6. data/lib/rspec/{matchers → expectations}/configuration.rb +44 -14
  7. data/lib/rspec/expectations/expectation_target.rb +71 -38
  8. data/lib/rspec/expectations/fail_with.rb +12 -44
  9. data/lib/rspec/expectations/handler.rb +135 -35
  10. data/lib/rspec/expectations/minitest_integration.rb +16 -0
  11. data/lib/rspec/expectations/syntax.rb +60 -94
  12. data/lib/rspec/expectations/version.rb +1 -1
  13. data/lib/rspec/expectations.rb +46 -24
  14. data/lib/rspec/matchers/aliased_matcher.rb +41 -0
  15. data/lib/rspec/matchers/built_in/all.rb +78 -0
  16. data/lib/rspec/matchers/built_in/base_matcher.rb +63 -15
  17. data/lib/rspec/matchers/built_in/be.rb +170 -79
  18. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  19. data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
  20. data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
  21. data/lib/rspec/matchers/built_in/be_within.rb +40 -16
  22. data/lib/rspec/matchers/built_in/change.rb +275 -74
  23. data/lib/rspec/matchers/built_in/compound.rb +122 -0
  24. data/lib/rspec/matchers/built_in/contain_exactly.rb +256 -0
  25. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  26. data/lib/rspec/matchers/built_in/eq.rb +60 -6
  27. data/lib/rspec/matchers/built_in/eql.rb +17 -5
  28. data/lib/rspec/matchers/built_in/equal.rb +55 -21
  29. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  30. data/lib/rspec/matchers/built_in/has.rb +71 -15
  31. data/lib/rspec/matchers/built_in/include.rb +58 -29
  32. data/lib/rspec/matchers/built_in/match.rb +19 -1
  33. data/lib/rspec/matchers/built_in/operators.rb +119 -0
  34. data/lib/rspec/matchers/built_in/output.rb +150 -0
  35. data/lib/rspec/matchers/built_in/raise_error.rb +91 -33
  36. data/lib/rspec/matchers/built_in/respond_to.rb +42 -20
  37. data/lib/rspec/matchers/built_in/satisfy.rb +19 -3
  38. data/lib/rspec/matchers/built_in/start_and_end_with.rb +54 -18
  39. data/lib/rspec/matchers/built_in/throw_symbol.rb +38 -8
  40. data/lib/rspec/matchers/built_in/yield.rb +237 -55
  41. data/lib/rspec/matchers/built_in.rb +45 -33
  42. data/lib/rspec/matchers/composable.rb +179 -0
  43. data/lib/rspec/matchers/dsl.rb +362 -5
  44. data/lib/rspec/matchers/generated_descriptions.rb +13 -6
  45. data/lib/rspec/matchers/matcher_delegator.rb +37 -0
  46. data/lib/rspec/matchers/pretty.rb +33 -20
  47. data/lib/rspec/matchers.rb +450 -227
  48. data.tar.gz.sig +1 -0
  49. metadata +100 -215
  50. metadata.gz.sig +0 -0
  51. data/features/README.md +0 -49
  52. data/features/Upgrade.md +0 -53
  53. data/features/built_in_matchers/README.md +0 -90
  54. data/features/built_in_matchers/be.feature +0 -175
  55. data/features/built_in_matchers/be_within.feature +0 -48
  56. data/features/built_in_matchers/cover.feature +0 -47
  57. data/features/built_in_matchers/end_with.feature +0 -48
  58. data/features/built_in_matchers/equality.feature +0 -139
  59. data/features/built_in_matchers/exist.feature +0 -45
  60. data/features/built_in_matchers/expect_change.feature +0 -59
  61. data/features/built_in_matchers/expect_error.feature +0 -144
  62. data/features/built_in_matchers/have.feature +0 -109
  63. data/features/built_in_matchers/include.feature +0 -174
  64. data/features/built_in_matchers/match.feature +0 -52
  65. data/features/built_in_matchers/operators.feature +0 -227
  66. data/features/built_in_matchers/predicates.feature +0 -137
  67. data/features/built_in_matchers/respond_to.feature +0 -84
  68. data/features/built_in_matchers/satisfy.feature +0 -33
  69. data/features/built_in_matchers/start_with.feature +0 -48
  70. data/features/built_in_matchers/throw_symbol.feature +0 -91
  71. data/features/built_in_matchers/types.feature +0 -116
  72. data/features/built_in_matchers/yield.feature +0 -146
  73. data/features/custom_matchers/access_running_example.feature +0 -53
  74. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  75. data/features/custom_matchers/define_matcher.feature +0 -368
  76. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
  77. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  78. data/features/customized_message.feature +0 -22
  79. data/features/diffing.feature +0 -85
  80. data/features/implicit_docstrings.feature +0 -52
  81. data/features/step_definitions/additional_cli_steps.rb +0 -22
  82. data/features/support/env.rb +0 -5
  83. data/features/syntax_configuration.feature +0 -68
  84. data/features/test_frameworks/test_unit.feature +0 -46
  85. data/lib/rspec/expectations/deprecation.rb +0 -38
  86. data/lib/rspec/expectations/differ.rb +0 -115
  87. data/lib/rspec/expectations/errors.rb +0 -9
  88. data/lib/rspec/expectations/extensions/array.rb +0 -9
  89. data/lib/rspec/expectations/extensions/object.rb +0 -39
  90. data/lib/rspec/expectations/extensions.rb +0 -2
  91. data/lib/rspec/matchers/be_close.rb +0 -9
  92. data/lib/rspec/matchers/built_in/have.rb +0 -108
  93. data/lib/rspec/matchers/built_in/match_array.rb +0 -51
  94. data/lib/rspec/matchers/compatibility.rb +0 -14
  95. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  96. data/lib/rspec/matchers/matcher.rb +0 -300
  97. data/lib/rspec/matchers/method_missing.rb +0 -12
  98. data/lib/rspec/matchers/operator_matcher.rb +0 -109
  99. data/lib/rspec-expectations.rb +0 -1
  100. data/spec/rspec/expectations/differ_spec.rb +0 -170
  101. data/spec/rspec/expectations/expectation_target_spec.rb +0 -75
  102. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  103. data/spec/rspec/expectations/fail_with_spec.rb +0 -95
  104. data/spec/rspec/expectations/handler_spec.rb +0 -206
  105. data/spec/rspec/expectations/syntax_spec.rb +0 -97
  106. data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
  107. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  108. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
  109. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
  110. data/spec/rspec/matchers/be_spec.rb +0 -516
  111. data/spec/rspec/matchers/be_within_spec.rb +0 -129
  112. data/spec/rspec/matchers/change_spec.rb +0 -542
  113. data/spec/rspec/matchers/configuration_spec.rb +0 -238
  114. data/spec/rspec/matchers/cover_spec.rb +0 -69
  115. data/spec/rspec/matchers/description_generation_spec.rb +0 -176
  116. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  117. data/spec/rspec/matchers/eq_spec.rb +0 -60
  118. data/spec/rspec/matchers/eql_spec.rb +0 -41
  119. data/spec/rspec/matchers/equal_spec.rb +0 -78
  120. data/spec/rspec/matchers/exist_spec.rb +0 -124
  121. data/spec/rspec/matchers/has_spec.rb +0 -122
  122. data/spec/rspec/matchers/have_spec.rb +0 -445
  123. data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
  124. data/spec/rspec/matchers/include_spec.rb +0 -531
  125. data/spec/rspec/matchers/match_array_spec.rb +0 -194
  126. data/spec/rspec/matchers/match_spec.rb +0 -61
  127. data/spec/rspec/matchers/matcher_spec.rb +0 -471
  128. data/spec/rspec/matchers/matchers_spec.rb +0 -37
  129. data/spec/rspec/matchers/method_missing_spec.rb +0 -28
  130. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
  131. data/spec/rspec/matchers/raise_error_spec.rb +0 -395
  132. data/spec/rspec/matchers/respond_to_spec.rb +0 -292
  133. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  134. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
  135. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  136. data/spec/rspec/matchers/yield_spec.rb +0 -437
  137. data/spec/spec_helper.rb +0 -54
  138. data/spec/support/classes.rb +0 -56
  139. data/spec/support/in_sub_process.rb +0 -38
  140. data/spec/support/matchers.rb +0 -22
  141. data/spec/support/ruby_version.rb +0 -10
  142. data/spec/support/shared_examples.rb +0 -13
@@ -1,223 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "operator matchers", :uses_should do
4
- describe "should ==" do
5
- it "delegates message to target" do
6
- subject = "apple"
7
- subject.should_receive(:==).with("apple").and_return(true)
8
- subject.should == "apple"
9
- end
10
-
11
- it "returns true on success" do
12
- subject = "apple"
13
- (subject.should == "apple").should be_true
14
- end
15
-
16
- it "fails when target.==(actual) returns false" do
17
- subject = "apple"
18
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: "orange"\n got: "apple" (using ==)], "orange", "apple")
19
- subject.should == "orange"
20
- end
21
- end
22
-
23
- describe "unsupported operators", :if => RUBY_VERSION.to_f == 1.9 do
24
- it "raises an appropriate error for should != expected" do
25
- expect {
26
- "apple".should != "pear"
27
- }.to raise_error(/does not support `should != expected`. Use `should_not == expected`/)
28
- end
29
-
30
- it "raises an appropriate error for should_not != expected" do
31
- expect {
32
- "apple".should_not != "pear"
33
- }.to raise_error(/does not support `should_not != expected`. Use `should == expected`/)
34
- end
35
-
36
- it "raises an appropriate error for should !~ expected" do
37
- expect {
38
- "apple".should !~ /regex/
39
- }.to raise_error(/does not support `should !~ expected`. Use `should_not =~ expected`/)
40
- end
41
-
42
- it "raises an appropriate error for should_not !~ expected" do
43
- expect {
44
- "apple".should_not !~ /regex/
45
- }.to raise_error(/does not support `should_not !~ expected`. Use `should =~ expected`/)
46
- end
47
- end
48
-
49
- describe "should_not ==" do
50
- it "delegates message to target" do
51
- subject = "orange"
52
- subject.should_receive(:==).with("apple").and_return(false)
53
- subject.should_not == "apple"
54
- end
55
-
56
- it "returns true on success" do
57
- subject = "apple"
58
- (subject.should_not == "orange").should be_false
59
- end
60
-
61
- it "fails when target.==(actual) returns false" do
62
- subject = "apple"
63
- RSpec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple"\n got: "apple"], "apple", "apple")
64
- subject.should_not == "apple"
65
- end
66
- end
67
-
68
- describe "should ===" do
69
- it "delegates message to target" do
70
- subject = "apple"
71
- subject.should_receive(:===).with("apple").and_return(true)
72
- subject.should === "apple"
73
- end
74
-
75
- it "fails when target.===(actual) returns false" do
76
- subject = "apple"
77
- subject.should_receive(:===).with("orange").and_return(false)
78
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: "orange"\n got: "apple" (using ===)], "orange", "apple")
79
- subject.should === "orange"
80
- end
81
- end
82
-
83
- describe "should_not ===" do
84
- it "delegates message to target" do
85
- subject = "orange"
86
- subject.should_receive(:===).with("apple").and_return(false)
87
- subject.should_not === "apple"
88
- end
89
-
90
- it "fails when target.===(actual) returns false" do
91
- subject = "apple"
92
- subject.should_receive(:===).with("apple").and_return(true)
93
- RSpec::Expectations.should_receive(:fail_with).with(%[expected not: === "apple"\n got: "apple"], "apple", "apple")
94
- subject.should_not === "apple"
95
- end
96
- end
97
-
98
- describe "should =~" do
99
- it "delegates message to target" do
100
- subject = "foo"
101
- subject.should_receive(:=~).with(/oo/).and_return(true)
102
- subject.should =~ /oo/
103
- end
104
-
105
- it "fails when target.=~(actual) returns false" do
106
- subject = "fu"
107
- subject.should_receive(:=~).with(/oo/).and_return(false)
108
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: /oo/\n got: "fu" (using =~)], /oo/, "fu")
109
- subject.should =~ /oo/
110
- end
111
- end
112
-
113
- describe "should_not =~" do
114
- it "delegates message to target" do
115
- subject = "fu"
116
- subject.should_receive(:=~).with(/oo/).and_return(false)
117
- subject.should_not =~ /oo/
118
- end
119
-
120
- it "fails when target.=~(actual) returns false" do
121
- subject = "foo"
122
- subject.should_receive(:=~).with(/oo/).and_return(true)
123
- RSpec::Expectations.should_receive(:fail_with).with(%[expected not: =~ /oo/\n got: "foo"], /oo/, "foo")
124
- subject.should_not =~ /oo/
125
- end
126
- end
127
-
128
- describe "should >" do
129
- it "passes if > passes" do
130
- 4.should > 3
131
- end
132
-
133
- it "fails if > fails" do
134
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: > 5\n got: 4], 5, 4)
135
- 4.should > 5
136
- end
137
- end
138
-
139
- describe "should >=" do
140
- it "passes if actual == expected" do
141
- 4.should >= 4
142
- end
143
-
144
- it "passes if actual > expected" do
145
- 4.should >= 3
146
- end
147
-
148
- it "fails if > fails" do
149
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: >= 5\n got: 4], 5, 4)
150
- 4.should >= 5
151
- end
152
- end
153
-
154
- describe "should <" do
155
- it "passes if < passes" do
156
- 4.should < 5
157
- end
158
-
159
- it "fails if > fails" do
160
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: < 3\n got: 4], 3, 4)
161
- 4.should < 3
162
- end
163
- end
164
-
165
- describe "should <=" do
166
- it "passes if actual == expected" do
167
- 4.should <= 4
168
- end
169
-
170
- it "passes if actual < expected" do
171
- 4.should <= 5
172
- end
173
-
174
- it "fails if > fails" do
175
- RSpec::Expectations.should_receive(:fail_with).with(%[expected: <= 3\n got: 4], 3, 4)
176
- 4.should <= 3
177
- end
178
- end
179
-
180
- describe "OperatorMatcher registry" do
181
- let(:custom_klass) { Class.new }
182
- let(:custom_subklass) { Class.new(custom_klass) }
183
-
184
- after {
185
- RSpec::Matchers::OperatorMatcher.unregister(custom_klass, "=~")
186
- }
187
-
188
- it "allows operator matchers to be registered for types" do
189
- RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match)
190
- expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match)
191
- end
192
-
193
- it "considers ancestors when finding an operator matcher" do
194
- RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match)
195
- expect(RSpec::Matchers::OperatorMatcher.get(custom_subklass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match)
196
- end
197
-
198
- it "returns nil if there is no matcher registered for a type" do
199
- expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to be_nil
200
- end
201
- end
202
-
203
- describe RSpec::Matchers::BuiltIn::PositiveOperatorMatcher do
204
- it "works when the target has implemented #send" do
205
- o = Object.new
206
- def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
207
- expect {
208
- o.should == o
209
- }.not_to raise_error
210
- end
211
- end
212
-
213
- describe RSpec::Matchers::BuiltIn::NegativeOperatorMatcher do
214
- it "works when the target has implemented #send" do
215
- o = Object.new
216
- def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
217
- expect {
218
- o.should_not == :foo
219
- }.not_to raise_error
220
- end
221
- end
222
- end
223
-
@@ -1,395 +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 "fails if nothing is raised" do
14
- expect {
15
- expect {}.to raise_error
16
- }.to fail_with("expected Exception but nothing was raised")
17
- end
18
- end
19
-
20
- describe "raise_exception aliased to raise_error" do
21
- it "passes if anything is raised" do
22
- expect {raise}.to raise_exception
23
- end
24
- end
25
-
26
- describe "expect { ... }.to raise_error {|err| ... }" do
27
- it "passes if there is an error" do
28
- ran = false
29
- expect { non_existent_method }.to raise_error {|e|
30
- ran = true
31
- }
32
- expect(ran).to be_true
33
- end
34
-
35
- it "passes the error to the block" do
36
- error = nil
37
- expect { non_existent_method }.to raise_error {|e|
38
- error = e
39
- }
40
- expect(error).to be_kind_of(NameError)
41
- end
42
- end
43
-
44
- describe "expect { ... }.to_not raise_error" do
45
- it "passes if nothing is raised" do
46
- expect {}.to_not raise_error
47
- end
48
-
49
- it "fails if anything is raised" do
50
- expect {
51
- expect { raise RuntimeError, "example message" }.to_not raise_error
52
- }.to fail_with(/expected no Exception, got #<RuntimeError: example message>/)
53
- end
54
-
55
- it 'includes the backtrace of the error that was raised in the error message' do
56
- expect {
57
- expect { raise "boom" }.not_to raise_error
58
- }.to raise_error { |e|
59
- backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
60
- expect(e.message).to include("with backtrace", backtrace_line)
61
- }
62
- end
63
-
64
- it 'formats the backtrace using the configured backtrace formatter' do
65
- RSpec::Matchers.configuration.backtrace_formatter.
66
- stub(:format_backtrace).
67
- and_return("formatted-backtrace")
68
-
69
- expect {
70
- expect { raise "boom" }.not_to raise_error
71
- }.to raise_error { |e|
72
- expect(e.message).to include("with backtrace", "formatted-backtrace")
73
- }
74
- end
75
- end
76
-
77
- describe "expect { ... }.to raise_error(message)" do
78
- it "passes if RuntimeError is raised with the right message" do
79
- expect {raise 'blah'}.to raise_error('blah')
80
- end
81
-
82
- it "passes if RuntimeError is raised with a matching message" do
83
- expect {raise 'blah'}.to raise_error(/blah/)
84
- end
85
-
86
- it "passes if any other error is raised with the right message" do
87
- expect {raise NameError.new('blah')}.to raise_error('blah')
88
- end
89
-
90
- it "fails if RuntimeError error is raised with the wrong message" do
91
- expect do
92
- expect {raise 'blarg'}.to raise_error('blah')
93
- end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
94
- end
95
-
96
- it "fails if any other error is raised with the wrong message" do
97
- expect do
98
- expect {raise NameError.new('blarg')}.to raise_error('blah')
99
- end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
100
- end
101
-
102
- it 'includes the backtrace of any other error in the failure message' do
103
- expect {
104
- expect { raise "boom" }.to raise_error(ArgumentError)
105
- }.to raise_error { |e|
106
- backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
107
- expect(e.message).to include("with backtrace", backtrace_line)
108
- }
109
- end
110
- end
111
-
112
- describe "expect { ... }.to_not raise_error(message)" do
113
- it "passes if RuntimeError error is raised with the different message" do
114
- expect {raise 'blarg'}.to_not raise_error('blah')
115
- end
116
-
117
- it "passes if any other error is raised with the wrong message" do
118
- expect {raise NameError.new('blarg')}.to_not raise_error('blah')
119
- end
120
-
121
- it "fails if RuntimeError is raised with message" do
122
- expect do
123
- expect {raise 'blah'}.to_not raise_error('blah')
124
- end.to fail_with(/expected no Exception with "blah", got #<RuntimeError: blah>/)
125
- end
126
-
127
- it "fails if any other error is raised with message" do
128
- expect do
129
- expect {raise NameError.new('blah')}.to_not raise_error('blah')
130
- end.to fail_with(/expected no Exception with "blah", got #<NameError: blah>/)
131
- end
132
- end
133
-
134
- describe "expect { ... }.to raise_error(NamedError)" do
135
- it "passes if named error is raised" do
136
- expect { non_existent_method }.to raise_error(NameError)
137
- end
138
-
139
- it "fails if nothing is raised" do
140
- expect {
141
- expect { }.to raise_error(NameError)
142
- }.to fail_with(/expected NameError but nothing was raised/)
143
- end
144
-
145
- it "fails if another error is raised (NameError)" do
146
- expect {
147
- expect { raise RuntimeError, "example message" }.to raise_error(NameError)
148
- }.to fail_with(/expected NameError, got #<RuntimeError: example message>/)
149
- end
150
-
151
- it "fails if another error is raised (NameError)" do
152
- expect {
153
- expect { load "non/existent/file" }.to raise_error(NameError)
154
- }.to fail_with(/expected NameError, got #<LoadError/)
155
- end
156
- end
157
-
158
- describe "expect { ... }.to_not raise_error(NamedError)" do
159
- it "passes if nothing is raised" do
160
- expect { }.to_not raise_error(NameError)
161
- end
162
-
163
- it "passes if another error is raised" do
164
- expect { raise }.to_not raise_error(NameError)
165
- end
166
-
167
- it "fails if named error is raised" do
168
- expect {
169
- expect { 1 + 'b' }.to_not raise_error(TypeError)
170
- }.to fail_with(/expected no TypeError, got #<TypeError: String can't be/)
171
- end
172
- end
173
-
174
- describe "expect { ... }.to raise_error(NamedError, error_message) with String" do
175
- it "passes if named error is raised with same message" do
176
- expect { raise "example message" }.to raise_error(RuntimeError, "example message")
177
- end
178
-
179
- it "fails if nothing is raised" do
180
- expect {
181
- expect {}.to raise_error(RuntimeError, "example message")
182
- }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
183
- end
184
-
185
- it "fails if incorrect error is raised" do
186
- expect {
187
- expect { raise RuntimeError, "example message" }.to raise_error(NameError, "example message")
188
- }.to fail_with(/expected NameError with \"example message\", got #<RuntimeError: example message>/)
189
- end
190
-
191
- it "fails if correct error is raised with incorrect message" do
192
- expect {
193
- expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, "example message")
194
- }.to fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
195
- end
196
- end
197
-
198
- describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }" do
199
- it "yields exception if named error is raised with same message" do
200
- ran = false
201
-
202
- expect {
203
- raise "example message"
204
- }.to raise_error(RuntimeError, "example message") { |err|
205
- ran = true
206
- expect(err.class).to eq RuntimeError
207
- expect(err.message).to eq "example message"
208
- }
209
-
210
- expect(ran).to be(true)
211
- end
212
-
213
- it "yielded block fails on it's own right" do
214
- ran, passed = false, false
215
-
216
- expect {
217
- expect {
218
- raise "example message"
219
- }.to raise_error(RuntimeError, "example message") { |err|
220
- ran = true
221
- expect(5).to eq 4
222
- passed = true
223
- }
224
- }.to fail_with(/expected: 4/m)
225
-
226
- expect(ran).to be_true
227
- expect(passed).to be_false
228
- end
229
-
230
- it "does NOT yield exception if no error was thrown" do
231
- ran = false
232
-
233
- expect {
234
- expect {}.to raise_error(RuntimeError, "example message") { |err|
235
- ran = true
236
- }
237
- }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
238
-
239
- expect(ran).to eq false
240
- end
241
-
242
- it "does not yield exception if error class is not matched" do
243
- ran = false
244
-
245
- expect {
246
- expect {
247
- raise "example message"
248
- }.to raise_error(SyntaxError, "example message") { |err|
249
- ran = true
250
- }
251
- }.to fail_with(/expected SyntaxError with \"example message\", got #<RuntimeError: example message>/)
252
-
253
- expect(ran).to eq false
254
- end
255
-
256
- it "does NOT yield exception if error message is not matched" do
257
- ran = false
258
-
259
- expect {
260
- expect {
261
- raise "example message"
262
- }.to raise_error(RuntimeError, "different message") { |err|
263
- ran = true
264
- }
265
- }.to fail_with(/expected RuntimeError with \"different message\", got #<RuntimeError: example message>/)
266
-
267
- expect(ran).to eq false
268
- end
269
- end
270
-
271
- describe "expect { ... }.to_not raise_error(NamedError, error_message) { |err| ... }" do
272
- it "passes if nothing is raised" do
273
- ran = false
274
-
275
- expect {}.to_not raise_error(RuntimeError, "example message") { |err|
276
- ran = true
277
- }
278
-
279
- expect(ran).to eq false
280
- end
281
-
282
- it "passes if a different error is raised" do
283
- ran = false
284
-
285
- expect { raise }.to_not raise_error(NameError, "example message") { |err|
286
- ran = true
287
- }
288
-
289
- expect(ran).to eq false
290
- end
291
-
292
- it "passes if same error is raised with different message" do
293
- ran = false
294
-
295
- expect {
296
- raise RuntimeError.new("not the example message")
297
- }.to_not raise_error(RuntimeError, "example message") { |err|
298
- ran = true
299
- }
300
-
301
- expect(ran).to eq false
302
- end
303
-
304
- it "fails if named error is raised with same message" do
305
- ran = false
306
-
307
- expect {
308
- expect {
309
- raise "example message"
310
- }.to_not raise_error(RuntimeError, "example message") { |err|
311
- ran = true
312
- }
313
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
314
-
315
- expect(ran).to eq false
316
- end
317
-
318
- it 'skips the error verification block when using the expect {...}.to syntax' do
319
- ran = false
320
-
321
- expect {
322
- expect {
323
- raise "example message"
324
- }.not_to raise_error(RuntimeError, "example message") { |err|
325
- ran = true
326
- }
327
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
328
-
329
- expect(ran).to eq false
330
- end
331
- end
332
-
333
- describe "expect { ... }.to_not raise_error(NamedError, error_message) with String" do
334
- it "passes if nothing is raised" do
335
- expect {}.to_not raise_error(RuntimeError, "example message")
336
- end
337
-
338
- it "passes if a different error is raised" do
339
- expect { raise }.to_not raise_error(NameError, "example message")
340
- end
341
-
342
- it "passes if same error is raised with different message" do
343
- expect { raise RuntimeError.new("not the example message") }.to_not raise_error(RuntimeError, "example message")
344
- end
345
-
346
- it "fails if named error is raised with same message" do
347
- expect {
348
- expect { raise "example message" }.to_not raise_error(RuntimeError, "example message")
349
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
350
- end
351
- end
352
-
353
- describe "expect { ... }.to raise_error(NamedError, error_message) with Regexp" do
354
- it "passes if named error is raised with matching message" do
355
- expect { raise "example message" }.to raise_error(RuntimeError, /ample mess/)
356
- end
357
-
358
- it "fails if nothing is raised" do
359
- expect {
360
- expect {}.to raise_error(RuntimeError, /ample mess/)
361
- }.to fail_with(/expected RuntimeError with message matching \/ample mess\/ but nothing was raised/)
362
- end
363
-
364
- it "fails if incorrect error is raised" do
365
- expect {
366
- expect { raise RuntimeError, "example message" }.to raise_error(NameError, /ample mess/)
367
- }.to fail_with(/expected NameError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
368
- end
369
-
370
- it "fails if correct error is raised with incorrect message" do
371
- expect {
372
- expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, /less than ample mess/)
373
- }.to fail_with(/expected RuntimeError with message matching \/less than ample mess\/, got #<RuntimeError: not the example message>/)
374
- end
375
- end
376
-
377
- describe "expect { ... }.to_not raise_error(NamedError, error_message) with Regexp" do
378
- it "passes if nothing is raised" do
379
- expect {}.to_not raise_error(RuntimeError, /ample mess/)
380
- end
381
-
382
- it "passes if a different error is raised" do
383
- expect { raise }.to_not raise_error(NameError, /ample mess/)
384
- end
385
-
386
- it "passes if same error is raised with non-matching message" do
387
- expect { raise RuntimeError.new("non matching message") }.to_not raise_error(RuntimeError, /ample mess/)
388
- end
389
-
390
- it "fails if named error is raised with matching message" do
391
- expect {
392
- expect { raise "example message" }.to_not raise_error(RuntimeError, /ample mess/)
393
- }.to fail_with(/expected no RuntimeError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
394
- end
395
- end