nunit Fluent Assertions Advanced Constraint Usage


Example

Large fluent assertions do become harder to read, but when combined with classes that have good implementations of ToString(), they can generate very useful error messages.

[Test]
public void AdvancedContraintsGiveUsefulErrorMessages() {
    Assert.That(actualCollection, Has
        .Count.EqualTo(4)
        .And.Exactly(1).Property("Age").GreaterThan(60)
        .And.Some.Property("Address").Null
        .And.No.Property("Age").LessThanOrEqualTo(17));
}

On failure, this assertion generates messages like this:

Expected: property Count equal to 4 and exactly one item property Age greater
than 60 and some item property Address null and not property Age less than or
equal to 17
But was:  < <Steve Taylor (23) lives in Newcastle
>, <Michelle Yung (65) lives in San Francisco
>, <Ranjit Saraman (49) lives in Milano
>, <LaChelle Oppenheimer (16) lives in 
> >