expectThrows

Asserts that the expression throws the specified throwable.

T
expectThrows
(
T : Throwable = Exception
E
)
(
lazy E expression
,
lazy string msg = null
,
string file = __FILE__
,
size_t line = __LINE__
)

Return Value

Type: T

the caught throwable

Throws

AssertException otherwise

Examples

import std.exception : enforce;

auto exception = expectThrows(enforce(false));

assertEquals("Enforcement failed", exception.msg);
auto exception = expectThrows!AssertException(expectThrows(42));

assertEquals("expected <Exception> was not thrown", exception.msg);

Meta