Dunit Assertions

Like most xUnit frameworks, dunit provides a set of assertions that you can use in your tests. Dunit's expect module supports a fluent interface, like that of NUnit, which can represent a large variety of constraints with few methods to recall.

As an example:

// [1, 2, 5] cannot contain 2 elements equal to 2
expect ([1, 2, 5]).not.has (2).equals (2);

API documentation

expect(T)(T value)

Start an assertion based on the given value.

expect.because (char[] message)

Start an assertion. If the assertion fails, the given message will be included in the error message. You must specify the object to test with that(T):

// exception message: Expected: less than 2 but was: 5 -- the value should be less than 2
expect.because ("the value should be less than 2").that (5).lessThan (2);

equals

Succeeds if the test item and the expected item compare as equal using the "==" operator.

expect(5).equals(5.0);
expect(this).equals(new Object());

sameAs

Succeeds if the test item and the expected item compare as equal using the "is" operator.

expect(5).sameAs(5.0);
expect(this).sameAs(new Object());

greaterThan

Succeeds if the test item is greater than the expected item.

expect(5).greaterThan(3);

lessThan

Succeeds if the test item is less than the expected item.

expect(5).lessThan(5.1);

has(int min, int max)

has(int count)

Succeeds if the test item is a collection containing the appropriate number of elements that match the constraints that follow.

expect([1, 2, 3]).has(1, 2).lessThan(3);
expect([1, 2, 3]).has(3).lessThan(4);

hasNone

Equivalent to has(0).

hasOne

Equivalent to has(1).

hasSome

Equivalent to has(1, int.max).

not

Negates the assertion. Anything appearing after it is negated; anything appearing before it is not negated. This has more of an effect in collection constraints:

// This one succeeds because the array doesn't have two things equal to 2
expect ([1, 2, 5]).not.has (2).equals (2);
// This one succeeds because the array has exactly 2 things equal to 2
expect ([1, 2, 5]).has (2).not.equals (2);

has..where (T)(bool delegate (T) sieve)

Succeeds if the test item is a collection of T that contains an appropriate number of items where sieve(item) returns true.

expect ([1, 2, 3, 4, 5]).has (3).where ((int i) { return i % 2 == 1; });

isNull

Succeeds if the original object compares same as null.

expect (null).isNull;

isNaN

Succeeds if isnan returns true for the original object.

expect (float.nan).isNaN;

isA(T)

Succeeds if the original object can be cast to an object of type T.

expect (new MyClass).isA!(MyBaseClass);