Question

I have a method that does temperature conversions.
The method is intended to raise a TypeError in certain cases.
How can I check for that in rspec? Right now the code below gives me:

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

Code:

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
Was it helpful?

Solution

expect { raise TypeError }.to raise TypeError

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top