Skip to content

Commit e000f76

Browse files
committed
Always specify the error expected to be raised
Using the `raise_error` matcher without providing a specific error or message risks false positives, since `raise_error` will match when Ruby raises a `NoMethodError`, `NameError` or `ArgumentError`, potentially allowing the expectation to pass without even executing the method you are intending to call.
1 parent e000e13 commit e000f76

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

spec/ruby-measurement/core_ext/string_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
describe 'with valid quantity and invalid unit' do
1616
subject { '3 people' }
17-
specify { expect { subject.to_measurement }.to raise_error }
17+
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
1818
end
1919

2020
describe 'with invalid input' do
2121
subject { 'foobar' }
22-
specify { expect { subject.to_measurement }.to raise_error }
22+
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
2323
end
2424
end
2525

@@ -31,7 +31,7 @@
3131

3232
describe 'with invalid unit' do
3333
subject { 'person' }
34-
specify { expect { subject.to_unit }.to raise_error }
34+
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
3535
end
3636
end
3737
end

spec/ruby-measurement/core_ext/symbol_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
describe 'with invalid unit' do
1111
subject { :person }
12-
specify { expect { subject.to_unit }.to raise_error }
12+
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
1313
end
1414
end
1515
end

spec/ruby-measurement/measurement_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838

3939
describe 'with invalid quantity' do
4040
it 'raises exception' do
41-
expect { subject.new('hi') }.to raise_exception
41+
expect { subject.new('hi') }.to raise_error(ArgumentError)
4242
end
4343
end
4444

4545
describe 'with invalid unit' do
4646
it 'raises exception' do
47-
expect { subject.new(3, :finklebaum) }.to raise_exception
47+
expect { subject.new(3, :finklebaum) }.to raise_error(ArgumentError)
4848
end
4949
end
5050
end
@@ -146,7 +146,7 @@
146146
end
147147

148148
it 'raises exception when undefined' do
149-
expect { subject.parse('3 finklebaums') }.to raise_error
149+
expect { subject.parse('3 finklebaums') }.to raise_error(ArgumentError)
150150
end
151151
end
152152
end
@@ -174,11 +174,11 @@
174174
end
175175

176176
it 'raises exception if unit exists and is not convertable' do
177-
expect { measurement.convert_to(:inches) }.to raise_error
177+
expect { measurement.convert_to(:inches) }.to raise_error(ArgumentError)
178178
end
179179

180180
it 'raises exception if unit does not exist' do
181-
expect { measurement.convert_to(:finklebaum) }.to raise_error
181+
expect { measurement.convert_to(:finklebaum) }.to raise_error(ArgumentError)
182182
end
183183
end
184184

@@ -222,7 +222,7 @@
222222
it 'raises exception for incompatible units' do
223223
other = subject.new(4, :inches)
224224
expect(other.unit).to_not eq measurement.unit
225-
expect { measurement + other }.to raise_error
225+
expect { measurement + other }.to raise_error(ArgumentError)
226226
end
227227
end
228228

@@ -256,7 +256,7 @@
256256
it 'raises exception for incompatible units' do
257257
other = subject.new(4, :inches)
258258
expect(other.unit).to_not eq measurement.unit
259-
expect { measurement - other }.to raise_error
259+
expect { measurement - other }.to raise_error(ArgumentError)
260260
end
261261
end
262262

@@ -290,7 +290,7 @@
290290
it 'raises exception for incompatible units' do
291291
other = subject.new(4, :inches)
292292
expect(other.unit).to_not eq measurement.unit
293-
expect { measurement * other }.to raise_error
293+
expect { measurement * other }.to raise_error(ArgumentError)
294294
end
295295
end
296296

@@ -324,7 +324,7 @@
324324
it 'raises exception for incompatible units' do
325325
other = subject.new(4, :inches)
326326
expect(other.unit).to_not eq measurement.unit
327-
expect { measurement / other }.to raise_error
327+
expect { measurement / other }.to raise_error(ArgumentError)
328328
end
329329
end
330330

@@ -336,7 +336,7 @@
336336
end
337337

338338
it 'raises exception for non-numeric values' do
339-
expect { measurement ** subject.new(3) }.to raise_error
339+
expect { measurement ** subject.new(3) }.to raise_error(ArgumentError)
340340
end
341341
end
342342

0 commit comments

Comments
 (0)