mercredi 18 février 2015

How to use assert_raise to test for an error in Ruby?

I have the programs factorial.rb and test_factorial.rb. The former looks like this:



def factorial(n)
(1..n).inject(:*) || 1
end


test_factorial looks like this:



def test_number
assert_equal 6, factorial(3),"3! should be 6"
end

def test_zero
assert_equal 1, factorial(0), "0! should be 1"
end


I need to add another method, test_negative to the test file that confirms an error is raised when the factorial is negative. I looked at the documentation but still don't understand what I'm supposed to do. Do I have to specify a certain type of error after assert_raise? Do I need to add code to factorial.rb or not since the error is generated automatically?


Aucun commentaire:

Enregistrer un commentaire