我有一种方法可以进行温度转换。
该方法旨在在某些情况下引发 TypeError 。
我如何在 rspec 中检查它?现在下面的代码给了我:
Failures:
1) It should raise TypeError for unknown conversion type 'blob_to_blob' should raise a TypeError when what_to_what param has the value 'blob_to_blob'
Failure/Error: raise TypeError
TypeError:
TypeError
# ./converter_spec.rb:7:in `converter'
# ./converter_spec.rb:25
Finished in 0.00134 seconds
3 examples, 1 failure
代码:
def converter(temperature,what_to_what)
if what_to_what == 'farenheit_to_centigrade'
(temperature-32)/2
elsif what_to_what == 'centigrade_to_farenheit'
(temperature*2)+ 32
else
raise TypeError
end
end
describe "It should be able to convert farenheit to centigrade" do
it "should convert 50 to 9" do
converter(50, 'farenheit_to_centigrade').should == 9
end
end
describe "It should be able to convert centigrade to farenheit" do
it "should convert 9 to 50" do
converter(9, 'centigrade_to_farenheit').should == 50
end
end
describe "It should raise TypeError for unknown conversion type 'blob_to_blob'" do
it "should raise a TypeError when what_to_what param has the value 'blob_to_blob'" do
converter(9, 'blob_to_blob').should raise TypeError
end
end