tags:

views:

27

answers:

1

I have been suggested to use should:rise in my test case to test for errors that a method might raise. For some reason it does not work as expected, so I want to verify that I'm doing it right. Here is the code in the test case:

self should: [aMyClass compareTo: 'This is a string'] raise: 'invalid input'.

My compareTo/1 method looks like this:

(aMyClass isKindOf: MyClass) ifFalse: [self error: 'invalid input'.].

The test runner output is that there is "1 errors".

Thank you.

+3  A: 

#should:raise: expects an Exception class as its second argument, similar to the first argument of #on:do: in exception handling:

 self should: [ aMyClass compareTo: 'This is a string' ] raise: Error
Lukas Renggli
How can I distinguish between different errors the compareTo might raise?I have just started learning squeak, so I am not yet familiar with error/exception mechanism.
Artium
To differentiate the errors nest the handlers: `[ [ aMyClass compareTo: 'This is a string' ] on: ErrorType1 do: [ :err | ... ] ] on: ErrorType2 do: [ :err | ... ]`. Check out the Pharo by Example 2 (http://www.pharobyexample.org/) draft chapter on exceptions: https://gforge.inria.fr/frs/download.php/26600/PBE2-Exceptions-2010-03-02.pdf.
Lukas Renggli