shouldThrow

Verify that expr throws the templated Exception class. This succeeds if the expression throws a child class of the template parameter.

shouldThrow
(
T : Throwable = Exception
E
)
(
lazy E expr
,
in string file = __FILE__
,
in size_t line = __LINE__
)

Return Value

Type: auto

The caught throwable.

Throws

UnitTestException on failure (when expr does not throw the expected exception)

Examples

1 import unit_threaded.asserts;
2 void funcThrows(string msg) { throw new Exception(msg); }
3 try {
4     auto exception = funcThrows("foo bar").shouldThrow;
5     assertEqual(exception.msg, "foo bar");
6 } catch(Exception e) {
7     assert(false, "should not have thrown anything and threw: " ~ e.msg);
8 }
1 import unit_threaded.asserts;
2 void func() {}
3 try {
4     func.shouldThrow;
5     assert(false, "Should never get here");
6 } catch(Exception e)
7     assertEqual(e.msg, "Expression did not throw");
1 import unit_threaded.asserts;
2 void funcAsserts() { assert(false); }
3 try {
4     funcAsserts.shouldThrow;
5     assert(false, "Should never get here");
6 } catch(Exception e)
7     assertEqual(e.msg, "Expression threw core.exception.AssertError instead of the expected Exception");

Meta