rspec-expectations 2.14.0 → 3.13.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 (155) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +1 -1
  5. data/Changelog.md +976 -25
  6. data/{License.txt → LICENSE.md} +5 -3
  7. data/README.md +162 -26
  8. data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
  9. data/lib/rspec/expectations/configuration.rb +230 -0
  10. data/lib/rspec/expectations/expectation_target.rb +127 -51
  11. data/lib/rspec/expectations/fail_with.rb +17 -57
  12. data/lib/rspec/expectations/failure_aggregator.rb +229 -0
  13. data/lib/rspec/expectations/handler.rb +146 -32
  14. data/lib/rspec/expectations/minitest_integration.rb +58 -0
  15. data/lib/rspec/expectations/syntax.rb +68 -100
  16. data/lib/rspec/expectations/version.rb +1 -1
  17. data/lib/rspec/expectations.rb +58 -23
  18. data/lib/rspec/matchers/aliased_matcher.rb +116 -0
  19. data/lib/rspec/matchers/built_in/all.rb +86 -0
  20. data/lib/rspec/matchers/built_in/base_matcher.rb +191 -20
  21. data/lib/rspec/matchers/built_in/be.rb +114 -114
  22. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  23. data/lib/rspec/matchers/built_in/be_instance_of.rb +15 -4
  24. data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
  25. data/lib/rspec/matchers/built_in/be_within.rb +35 -18
  26. data/lib/rspec/matchers/built_in/change.rb +389 -80
  27. data/lib/rspec/matchers/built_in/compound.rb +290 -0
  28. data/lib/rspec/matchers/built_in/contain_exactly.rb +310 -0
  29. data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
  30. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  31. data/lib/rspec/matchers/built_in/eq.rb +30 -8
  32. data/lib/rspec/matchers/built_in/eql.rb +23 -8
  33. data/lib/rspec/matchers/built_in/equal.rb +55 -22
  34. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  35. data/lib/rspec/matchers/built_in/has.rb +141 -22
  36. data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
  37. data/lib/rspec/matchers/built_in/include.rb +184 -32
  38. data/lib/rspec/matchers/built_in/match.rb +95 -1
  39. data/lib/rspec/matchers/built_in/operators.rb +128 -0
  40. data/lib/rspec/matchers/built_in/output.rb +207 -0
  41. data/lib/rspec/matchers/built_in/raise_error.rb +192 -44
  42. data/lib/rspec/matchers/built_in/respond_to.rb +154 -28
  43. data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
  44. data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
  45. data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
  46. data/lib/rspec/matchers/built_in/yield.rb +240 -161
  47. data/lib/rspec/matchers/built_in.rb +47 -33
  48. data/lib/rspec/matchers/composable.rb +171 -0
  49. data/lib/rspec/matchers/dsl.rb +531 -10
  50. data/lib/rspec/matchers/english_phrasing.rb +58 -0
  51. data/lib/rspec/matchers/fail_matchers.rb +42 -0
  52. data/lib/rspec/matchers/generated_descriptions.rb +14 -8
  53. data/lib/rspec/matchers/matcher_delegator.rb +61 -0
  54. data/lib/rspec/matchers/matcher_protocol.rb +105 -0
  55. data/lib/rspec/matchers/multi_matcher_diff.rb +82 -0
  56. data/lib/rspec/matchers.rb +520 -173
  57. data.tar.gz.sig +0 -0
  58. metadata +141 -242
  59. metadata.gz.sig +2 -0
  60. data/features/README.md +0 -48
  61. data/features/Upgrade.md +0 -53
  62. data/features/built_in_matchers/README.md +0 -90
  63. data/features/built_in_matchers/be.feature +0 -175
  64. data/features/built_in_matchers/be_within.feature +0 -48
  65. data/features/built_in_matchers/cover.feature +0 -47
  66. data/features/built_in_matchers/end_with.feature +0 -48
  67. data/features/built_in_matchers/equality.feature +0 -139
  68. data/features/built_in_matchers/exist.feature +0 -45
  69. data/features/built_in_matchers/expect_change.feature +0 -59
  70. data/features/built_in_matchers/expect_error.feature +0 -144
  71. data/features/built_in_matchers/have.feature +0 -109
  72. data/features/built_in_matchers/include.feature +0 -174
  73. data/features/built_in_matchers/match.feature +0 -52
  74. data/features/built_in_matchers/operators.feature +0 -227
  75. data/features/built_in_matchers/predicates.feature +0 -137
  76. data/features/built_in_matchers/respond_to.feature +0 -84
  77. data/features/built_in_matchers/satisfy.feature +0 -33
  78. data/features/built_in_matchers/start_with.feature +0 -48
  79. data/features/built_in_matchers/throw_symbol.feature +0 -91
  80. data/features/built_in_matchers/types.feature +0 -116
  81. data/features/built_in_matchers/yield.feature +0 -161
  82. data/features/custom_matchers/access_running_example.feature +0 -53
  83. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  84. data/features/custom_matchers/define_matcher.feature +0 -368
  85. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
  86. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  87. data/features/customized_message.feature +0 -22
  88. data/features/diffing.feature +0 -85
  89. data/features/implicit_docstrings.feature +0 -52
  90. data/features/step_definitions/additional_cli_steps.rb +0 -22
  91. data/features/support/env.rb +0 -14
  92. data/features/syntax_configuration.feature +0 -71
  93. data/features/test_frameworks/test_unit.feature +0 -44
  94. data/lib/rspec/expectations/deprecation.rb +0 -17
  95. data/lib/rspec/expectations/differ.rb +0 -133
  96. data/lib/rspec/expectations/errors.rb +0 -9
  97. data/lib/rspec/expectations/extensions/array.rb +0 -9
  98. data/lib/rspec/expectations/extensions/object.rb +0 -29
  99. data/lib/rspec/expectations/extensions.rb +0 -2
  100. data/lib/rspec/matchers/be_close.rb +0 -9
  101. data/lib/rspec/matchers/built_in/have.rb +0 -124
  102. data/lib/rspec/matchers/built_in/match_array.rb +0 -51
  103. data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
  104. data/lib/rspec/matchers/compatibility.rb +0 -14
  105. data/lib/rspec/matchers/configuration.rb +0 -108
  106. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  107. data/lib/rspec/matchers/matcher.rb +0 -300
  108. data/lib/rspec/matchers/method_missing.rb +0 -12
  109. data/lib/rspec/matchers/operator_matcher.rb +0 -109
  110. data/lib/rspec/matchers/pretty.rb +0 -70
  111. data/lib/rspec/matchers/test_unit_integration.rb +0 -11
  112. data/lib/rspec-expectations.rb +0 -1
  113. data/spec/rspec/expectations/differ_spec.rb +0 -192
  114. data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
  115. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  116. data/spec/rspec/expectations/fail_with_spec.rb +0 -114
  117. data/spec/rspec/expectations/handler_spec.rb +0 -227
  118. data/spec/rspec/expectations/syntax_spec.rb +0 -139
  119. data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
  120. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  121. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
  122. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
  123. data/spec/rspec/matchers/be_spec.rb +0 -516
  124. data/spec/rspec/matchers/be_within_spec.rb +0 -137
  125. data/spec/rspec/matchers/change_spec.rb +0 -553
  126. data/spec/rspec/matchers/configuration_spec.rb +0 -206
  127. data/spec/rspec/matchers/cover_spec.rb +0 -69
  128. data/spec/rspec/matchers/description_generation_spec.rb +0 -190
  129. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  130. data/spec/rspec/matchers/eq_spec.rb +0 -60
  131. data/spec/rspec/matchers/eql_spec.rb +0 -41
  132. data/spec/rspec/matchers/equal_spec.rb +0 -78
  133. data/spec/rspec/matchers/exist_spec.rb +0 -124
  134. data/spec/rspec/matchers/has_spec.rb +0 -122
  135. data/spec/rspec/matchers/have_spec.rb +0 -455
  136. data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
  137. data/spec/rspec/matchers/include_spec.rb +0 -531
  138. data/spec/rspec/matchers/match_array_spec.rb +0 -194
  139. data/spec/rspec/matchers/match_spec.rb +0 -61
  140. data/spec/rspec/matchers/matcher_spec.rb +0 -471
  141. data/spec/rspec/matchers/matchers_spec.rb +0 -37
  142. data/spec/rspec/matchers/method_missing_spec.rb +0 -28
  143. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
  144. data/spec/rspec/matchers/raise_error_spec.rb +0 -485
  145. data/spec/rspec/matchers/respond_to_spec.rb +0 -292
  146. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  147. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
  148. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  149. data/spec/rspec/matchers/yield_spec.rb +0 -514
  150. data/spec/spec_helper.rb +0 -54
  151. data/spec/support/classes.rb +0 -56
  152. data/spec/support/in_sub_process.rb +0 -38
  153. data/spec/support/matchers.rb +0 -22
  154. data/spec/support/ruby_version.rb +0 -10
  155. 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,485 +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 :bees
20
- to_raise = StandardError.new(:faces)
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_true
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 { ... }.not_to raise_error" do
64
-
65
- context "with a specific error class" do
66
- it "is deprecated" do
67
- RSpec.should_receive :deprecate
68
- expect {"bees"}.not_to raise_error(RuntimeError)
69
- end
70
- end
71
-
72
- context "with no specific error class" do
73
- it "is not deprecated" do
74
- run = nil
75
- allow(RSpec).to receive(:deprecate) { run = true }
76
- expect {"bees"}.not_to raise_error
77
- expect(run).to be_nil
78
- end
79
-
80
- it "passes if nothing is raised" do
81
- expect {}.not_to raise_error
82
- end
83
-
84
- it "fails if anything is raised" do
85
- expect {
86
- expect { raise RuntimeError, "example message" }.not_to raise_error
87
- }.to fail_with(/expected no Exception, got #<RuntimeError: example message>/)
88
- end
89
-
90
- it 'includes the backtrace of the error that was raised in the error message' do
91
- expect {
92
- expect { raise "boom" }.not_to raise_error
93
- }.to raise_error { |e|
94
- backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
95
- expect(e.message).to include("with backtrace", backtrace_line)
96
- }
97
- end
98
-
99
- it 'formats the backtrace using the configured backtrace formatter' do
100
- RSpec::Matchers.configuration.backtrace_formatter.
101
- stub(:format_backtrace).
102
- and_return("formatted-backtrace")
103
-
104
- expect {
105
- expect { raise "boom" }.not_to raise_error
106
- }.to raise_error { |e|
107
- expect(e.message).to include("with backtrace", "formatted-backtrace")
108
- }
109
- end
110
- end
111
- end
112
-
113
- describe "expect { ... }.to raise_error(message)" do
114
- it "passes if RuntimeError is raised with the right message" do
115
- expect {raise 'blah'}.to raise_error('blah')
116
- end
117
-
118
- it "passes if RuntimeError is raised with a matching message" do
119
- expect {raise 'blah'}.to raise_error(/blah/)
120
- end
121
-
122
- it "passes if any other error is raised with the right message" do
123
- expect {raise NameError.new('blah')}.to raise_error('blah')
124
- end
125
-
126
- it "fails if RuntimeError error is raised with the wrong message" do
127
- expect do
128
- expect {raise 'blarg'}.to raise_error('blah')
129
- end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
130
- end
131
-
132
- it "fails if any other error is raised with the wrong message" do
133
- expect do
134
- expect {raise NameError.new('blarg')}.to raise_error('blah')
135
- end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
136
- end
137
-
138
- it 'includes the backtrace of any other error in the failure message' do
139
- expect {
140
- expect { raise "boom" }.to raise_error(ArgumentError)
141
- }.to raise_error { |e|
142
- backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
143
- expect(e.message).to include("with backtrace", backtrace_line)
144
- }
145
- end
146
- end
147
-
148
- describe "expect { ... }.not_to raise_error(message)" do
149
- before do
150
- allow(RSpec).to receive(:deprecate)
151
- end
152
-
153
- it "is deprecated" do
154
- expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(message\)/, :replacement =>"`expect { }.not_to raise_error()`")
155
- expect {raise 'blarg'}.not_to raise_error('blah')
156
- end
157
-
158
- it "passes if RuntimeError error is raised with the different message" do
159
- expect {raise 'blarg'}.not_to raise_error('blah')
160
- end
161
-
162
- it "passes if any other error is raised with the wrong message" do
163
- expect {raise NameError.new('blarg')}.not_to raise_error('blah')
164
- end
165
-
166
- it "fails if RuntimeError is raised with message" do
167
- expect do
168
- expect {raise 'blah'}.not_to raise_error('blah')
169
- end.to fail_with(/expected no Exception with "blah", got #<RuntimeError: blah>/)
170
- end
171
-
172
- it "fails if any other error is raised with message" do
173
- expect do
174
- expect {raise NameError.new('blah')}.not_to raise_error('blah')
175
- end.to fail_with(/expected no Exception with "blah", got #<NameError: blah>/)
176
- end
177
- end
178
-
179
- describe "expect { ... }.to raise_error(NamedError)" do
180
- it "passes if named error is raised" do
181
- expect { non_existent_method }.to raise_error(NameError)
182
- end
183
-
184
- it "fails if nothing is raised" do
185
- expect {
186
- expect { }.to raise_error(NameError)
187
- }.to fail_with(/expected NameError but nothing was raised/)
188
- end
189
-
190
- it "fails if another error is raised (NameError)" do
191
- expect {
192
- expect { raise RuntimeError, "example message" }.to raise_error(NameError)
193
- }.to fail_with(/expected NameError, got #<RuntimeError: example message>/)
194
- end
195
-
196
- it "fails if another error is raised (NameError)" do
197
- expect {
198
- expect { load "non/existent/file" }.to raise_error(NameError)
199
- }.to fail_with(/expected NameError, got #<LoadError/)
200
- end
201
- end
202
-
203
- describe "expect { ... }.not_to raise_error(NamedError)" do
204
- before do
205
- allow(RSpec).to receive(:deprecate)
206
- end
207
-
208
- it "is deprecated" do
209
- expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass\)/, :replacement => "`expect { }.not_to raise_error()`")
210
- expect { }.not_to raise_error(NameError)
211
- end
212
-
213
- it "passes if nothing is raised" do
214
- expect { }.not_to raise_error(NameError)
215
- end
216
-
217
- it "passes if another error is raised" do
218
- expect { raise }.not_to raise_error(NameError)
219
- end
220
-
221
- it "fails if named error is raised" do
222
- expect {
223
- expect { 1 + 'b' }.not_to raise_error(TypeError)
224
- }.to fail_with(/expected no TypeError, got #<TypeError: String can't be/)
225
- end
226
- end
227
-
228
- describe "expect { ... }.to raise_error(NamedError, error_message) with String" do
229
- it "passes if named error is raised with same message" do
230
- expect { raise "example message" }.to raise_error(RuntimeError, "example message")
231
- end
232
-
233
- it "fails if nothing is raised" do
234
- expect {
235
- expect {}.to raise_error(RuntimeError, "example message")
236
- }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
237
- end
238
-
239
- it "fails if incorrect error is raised" do
240
- expect {
241
- expect { raise RuntimeError, "example message" }.to raise_error(NameError, "example message")
242
- }.to fail_with(/expected NameError with \"example message\", got #<RuntimeError: example message>/)
243
- end
244
-
245
- it "fails if correct error is raised with incorrect message" do
246
- expect {
247
- expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, "example message")
248
- }.to fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
249
- end
250
- end
251
-
252
- describe "expect { ... }.not_to raise_error(NamedError, error_message) with String" do
253
- before do
254
- allow(RSpec).to receive(:deprecate)
255
- end
256
-
257
- it "is deprecated" do
258
- expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass, message\)/, :replacement =>"`expect { }.not_to raise_error()`")
259
- expect {}.not_to raise_error(RuntimeError, "example message")
260
- end
261
-
262
- it "passes if nothing is raised" do
263
- expect {}.not_to raise_error(RuntimeError, "example message")
264
- end
265
-
266
- it "passes if a different error is raised" do
267
- expect { raise }.not_to raise_error(NameError, "example message")
268
- end
269
-
270
- it "passes if same error is raised with different message" do
271
- expect { raise RuntimeError.new("not the example message") }.not_to raise_error(RuntimeError, "example message")
272
- end
273
-
274
- it "fails if named error is raised with same message" do
275
- expect {
276
- expect { raise "example message" }.not_to raise_error(RuntimeError, "example message")
277
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
278
- end
279
- end
280
-
281
- describe "expect { ... }.to raise_error(NamedError, error_message) with Regexp" do
282
- it "passes if named error is raised with matching message" do
283
- expect { raise "example message" }.to raise_error(RuntimeError, /ample mess/)
284
- end
285
-
286
- it "fails if nothing is raised" do
287
- expect {
288
- expect {}.to raise_error(RuntimeError, /ample mess/)
289
- }.to fail_with(/expected RuntimeError with message matching \/ample mess\/ but nothing was raised/)
290
- end
291
-
292
- it "fails if incorrect error is raised" do
293
- expect {
294
- expect { raise RuntimeError, "example message" }.to raise_error(NameError, /ample mess/)
295
- }.to fail_with(/expected NameError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
296
- end
297
-
298
- it "fails if correct error is raised with incorrect message" do
299
- expect {
300
- expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, /less than ample mess/)
301
- }.to fail_with(/expected RuntimeError with message matching \/less than ample mess\/, got #<RuntimeError: not the example message>/)
302
- end
303
- end
304
-
305
- describe "expect { ... }.not_to raise_error(NamedError, error_message) with Regexp" do
306
- before do
307
- allow(RSpec).to receive(:deprecate)
308
- end
309
-
310
- it "is deprecated" do
311
- expect(RSpec).to receive(:deprecate)
312
- expect {}.not_to raise_error(RuntimeError, /ample mess/)
313
- end
314
-
315
- it "passes if nothing is raised" do
316
- expect {}.not_to raise_error(RuntimeError, /ample mess/)
317
- end
318
-
319
- it "passes if a different error is raised" do
320
- expect { raise }.not_to raise_error(NameError, /ample mess/)
321
- end
322
-
323
- it "passes if same error is raised with non-matching message" do
324
- expect { raise RuntimeError.new("non matching message") }.not_to raise_error(RuntimeError, /ample mess/)
325
- end
326
-
327
- it "fails if named error is raised with matching message" do
328
- expect {
329
- expect { raise "example message" }.not_to raise_error(RuntimeError, /ample mess/)
330
- }.to fail_with(/expected no RuntimeError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
331
- end
332
- end
333
-
334
- describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }" do
335
- it "yields exception if named error is raised with same message" do
336
- ran = false
337
-
338
- expect {
339
- raise "example message"
340
- }.to raise_error(RuntimeError, "example message") { |err|
341
- ran = true
342
- expect(err.class).to eq RuntimeError
343
- expect(err.message).to eq "example message"
344
- }
345
-
346
- expect(ran).to be(true)
347
- end
348
-
349
- it "yielded block fails on it's own right" do
350
- ran, passed = false, false
351
-
352
- expect {
353
- expect {
354
- raise "example message"
355
- }.to raise_error(RuntimeError, "example message") { |err|
356
- ran = true
357
- expect(5).to eq 4
358
- passed = true
359
- }
360
- }.to fail_with(/expected: 4/m)
361
-
362
- expect(ran).to be_true
363
- expect(passed).to be_false
364
- end
365
-
366
- it "does NOT yield exception if no error was thrown" do
367
- ran = false
368
-
369
- expect {
370
- expect {}.to raise_error(RuntimeError, "example message") { |err|
371
- ran = true
372
- }
373
- }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
374
-
375
- expect(ran).to eq false
376
- end
377
-
378
- it "does not yield exception if error class is not matched" do
379
- ran = false
380
-
381
- expect {
382
- expect {
383
- raise "example message"
384
- }.to raise_error(SyntaxError, "example message") { |err|
385
- ran = true
386
- }
387
- }.to fail_with(/expected SyntaxError with \"example message\", got #<RuntimeError: example message>/)
388
-
389
- expect(ran).to eq false
390
- end
391
-
392
- it "does NOT yield exception if error message is not matched" do
393
- ran = false
394
-
395
- expect {
396
- expect {
397
- raise "example message"
398
- }.to raise_error(RuntimeError, "different message") { |err|
399
- ran = true
400
- }
401
- }.to fail_with(/expected RuntimeError with \"different message\", got #<RuntimeError: example message>/)
402
-
403
- expect(ran).to eq false
404
- end
405
- end
406
-
407
- describe "expect { ... }.not_to raise_error(NamedError, error_message) { |err| ... }" do
408
- before do
409
- allow(RSpec).to receive(:deprecate)
410
- end
411
-
412
- it "is deprecated" do
413
- expect(RSpec).to receive(:deprecate)
414
- expect {}.not_to raise_error(RuntimeError, "example message") { |err| }
415
- end
416
-
417
- it "passes if nothing is raised" do
418
- ran = false
419
-
420
- expect {}.not_to raise_error(RuntimeError, "example message") { |err|
421
- ran = true
422
- }
423
-
424
- expect(ran).to eq false
425
- end
426
-
427
- it "passes if a different error is raised" do
428
- ran = false
429
-
430
- expect { raise }.not_to raise_error(NameError, "example message") { |err|
431
- ran = true
432
- }
433
-
434
- expect(ran).to eq false
435
- end
436
-
437
- it "passes if same error is raised with different message" do
438
- ran = false
439
-
440
- expect {
441
- raise RuntimeError.new("not the example message")
442
- }.not_to raise_error(RuntimeError, "example message") { |err|
443
- ran = true
444
- }
445
-
446
- expect(ran).to eq false
447
- end
448
-
449
- it "fails if named error is raised with same message" do
450
- ran = false
451
-
452
- expect {
453
- expect {
454
- raise "example message"
455
- }.not_to raise_error(RuntimeError, "example message") { |err|
456
- ran = true
457
- }
458
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
459
-
460
- expect(ran).to eq false
461
- end
462
-
463
- it 'skips the error verification block when using the expect {...}.to syntax' do
464
- ran = false
465
-
466
- expect {
467
- expect {
468
- raise "example message"
469
- }.not_to raise_error(RuntimeError, "example message") { |err|
470
- ran = true
471
- }
472
- }.to fail_with(/expected no RuntimeError with \"example message\", got #<RuntimeError: example message>/)
473
-
474
- expect(ran).to eq false
475
- end
476
- end
477
-
478
- describe "misuse of raise_error, with (), not {}" do
479
- it "fails with warning" do
480
- ::Kernel.should_receive(:warn).with(/`raise_error` was called with non-proc object 1\.7/)
481
- expect {
482
- expect(Math.sqrt(3)).to raise_error
483
- }.to fail_with(/nothing was raised/)
484
- end
485
- end