Skip to content

Commit

Permalink
Always specify the error expected to be raised
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sferik committed Mar 28, 2016
1 parent e000e13 commit e0004e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions spec/ruby-measurement/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

describe 'with valid quantity and invalid unit' do
subject { '3 people' }
specify { expect { subject.to_measurement }.to raise_error }
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
end

describe 'with invalid input' do
subject { 'foobar' }
specify { expect { subject.to_measurement }.to raise_error }
specify { expect { subject.to_measurement }.to raise_error(ArgumentError) }
end
end

Expand All @@ -31,7 +31,7 @@

describe 'with invalid unit' do
subject { 'person' }
specify { expect { subject.to_unit }.to raise_error }
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
end
end
end
2 changes: 1 addition & 1 deletion spec/ruby-measurement/core_ext/symbol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

describe 'with invalid unit' do
subject { :person }
specify { expect { subject.to_unit }.to raise_error }
specify { expect { subject.to_unit }.to raise_error(ArgumentError) }
end
end
end
20 changes: 10 additions & 10 deletions spec/ruby-measurement/measurement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

describe 'with invalid quantity' do
it 'raises exception' do
expect { subject.new('hi') }.to raise_exception
expect { subject.new('hi') }.to raise_error(ArgumentError)
end
end

describe 'with invalid unit' do
it 'raises exception' do
expect { subject.new(3, :finklebaum) }.to raise_exception
expect { subject.new(3, :finklebaum) }.to raise_error(ArgumentError)
end
end
end
Expand Down Expand Up @@ -146,7 +146,7 @@
end

it 'raises exception when undefined' do
expect { subject.parse('3 finklebaums') }.to raise_error
expect { subject.parse('3 finklebaums') }.to raise_error(ArgumentError)
end
end
end
Expand Down Expand Up @@ -174,11 +174,11 @@
end

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

it 'raises exception if unit does not exist' do
expect { measurement.convert_to(:finklebaum) }.to raise_error
expect { measurement.convert_to(:finklebaum) }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -222,7 +222,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement + other }.to raise_error
expect { measurement + other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -256,7 +256,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement - other }.to raise_error
expect { measurement - other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -290,7 +290,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement * other }.to raise_error
expect { measurement * other }.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -324,7 +324,7 @@
it 'raises exception for incompatible units' do
other = subject.new(4, :inches)
expect(other.unit).to_not eq measurement.unit
expect { measurement / other }.to raise_error
expect { measurement / other }.to raise_error(ArgumentError)
end
end

Expand All @@ -336,7 +336,7 @@
end

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

Expand Down

0 comments on commit e0004e7

Please sign in to comment.