shouldThrow

Used for asserting that a delegate will throw an exception.

void
shouldThrow
(
void delegate(
)
cb
,
string message = null
,
string file = __FILE__
,
size_t line = __LINE__
)

Parameters

cb
Type: void delegate(
)

The delegate that is expected to throw the exception.

message
Type: string

The message that is expected to be in the exception. Will not be tested, if it is null.

file
Type: string

The file name that the assert failed in. Should be left as default.

line
Type: size_t

The file line that the assert failed in. Should be left as default.

Throws

If delegate does NOT throw, will throw an AssertError.

Examples

1 // Makes sure it throws with the message "boom!"
2 shouldThrow(delegate() {
3 	throw new Exception("boom!");
4 }, "boom!");
5 
6 // Makes sure it throws, but does not check the message
7 shouldThrow(delegate() {
8 	throw new Exception("boom!");
9 });
10 
11 // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected: boom!"
12 shouldThrow(delegate() {
13 
14 }, "boom!");
15 
16 // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected one.
17 shouldThrow(delegate() {
18 
19 });

Meta