marick/Midje — Issue #62: Feature request: throws checker that can check arbitrary properties of exceptions
Author: pjnagel
Created: 2011-11-09 12:29:37+00:00
State: open

Currently, the (throws SomeException) checker is only satisfied if an exception of exactly the class SomeException is thrown, and not if an exception that is a subclass of SomeException is thrown.

Based on experience with other testing frameworks in other languages, I find that one often wants to check that an exception satisfies arbitrary properties beyond just its class and its text message. For example, an exception might be designed to carry a data payload beyond just an error string, and one might want to check that.

So one way of satisfying all these desires would be if the throws checker can optionally take any predicate as parameter, and it will only be satisfied if an exception is actually thrown and that exception makes the predicate return true.

For example:
(something) => (throws #(= (.rocket-velocity %) 100))

Or even:
(something) => (throws SomeException #(= (.payload %) {:foo 1, :bar 2}))

The most elegant solution would be if one could place an arbitrary checker after (throws, and that checker in turn would be checked against the thrown exception.