- Identify what are diff. use cases we need to test, and define 'test' method for each. For the above example, we need,
- breaking_fact
- test for 0
- test for 1
- test for any other number (say 3)
- test for big number, which should break and throw exception.
- fact
- test for 0
- test for 1
- test for any other number
- test for big number.
- Use assertions to test the outcome, with appropriate message. All cases should cover all possible cases by which we can call the method, i.e all positive/negative cases.
- Test it using command 'ruby factorial_test.rb'
Thanks @zenspider for suggesting the improvements.
assert !Factorial.fact(8999).nil?, "should be a number”
doesn’t test that it is a number. Just not nil.assert_equal
andrefute_equal
to get good error messages.