0
我有一個簡單的功能。RSpec失敗raise_error
def check_num(num)
if num.is_a?(Integer) && num > 0
#...
else
raise 'NOT VALID'
end
end
,並試圖與下面的測試使用RSpec來測試它:
require 'find'
describe 'check_num' do
describe 'errors' do
it 'raises an error if parameter is 0' do
expect(check_num(0)).to raise_error(RuntimeError)
end
it 'raises an error if parameter is less than 0' do
expect(check_num(-1)).to raise_error(RuntimeError)
end
it 'raises an error if parameter is not a number' do
expect(check_num('Heya, Am a string')).to raise_error(RuntimeError)
end
end
end
而且這是我和我的測試得到:
/home/duke/.rvm/rubies/ruby-2.2.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/duke/.rvm/gems/ruby-2.2.3/bin/rspec /home/duke/RubymineProjects/rspec_tutor/prime_numbers/spec/find_spec.rb --require teamcity/spec/runner/formatter/teamcity/formatter --format Spec::Runner::Formatter::TeamcityFormatter
Testing started at 15:04 ...
RuntimeError: NOT VALID
./lib/find.rb:37:in `check_num'
./spec/find_spec.rb:8:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
RuntimeError: NOT VALID
./lib/find.rb:37:in `check_num'
./spec/find_spec.rb:12:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
ArgumentError: comparison of String with 0 failed
./lib/find.rb:28:in `>'
./lib/find.rb:28:in `check_num'
./spec/find_spec.rb:16:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
3 examples, 3 failures, 0 passed
Finished in 0.004547262 seconds
Process finished with exit code 1
爲什麼我收到一個錯誤而raise_error
是我正在測試的內容? 我應該如何測試錯誤提升呢?