I’ve got a function:
unify :: [Constraint] -> [Substitution]
and in certain cases it throws exceptions with the error function:
error "Circular constraint"
I’m using Test.HUnit for unit testing and I’d like to make a test case that asserts these errors are thrown on certain inputs. I found this, which provides a way of testing for exceptions that are instances of Eq, but error seems to give an ErrorCall exception, which is not an instance of Eq, so I get the error:
No instance for (Eq ErrorCall)
arising from a use of `assertException'
How can I write a TestCase that asserts that error was called and (preferably) checks the message?
Ideally I’d refactor your function into
I would then test
unify'instead of testingunify.If there was more than one possible error message, I would refactor it like this instead:
(Incidentally, if this is for a library other programmers will be using, some of them would prefer to call
unify'instead of the partial functionunify.)If you can’t refactor your code, I’d modify the code you link to, replacing
assertExceptionwith: