Using Lambdas as Constraints in NUnit 2.5

Published: Sunday, May 3, 2009

Let's say you have an array of ints representing years, all of which should be leap years.

One way to test this would be to write a custom constraint, LeapYearConstraint. You could then use it with the Matches syntax element to write your test as

Assert.That( array, Is.All.Matches( new LeapYearConstraint() );

But creating a new constraint for this adhoc problem seems like a bit of overkill. Instead, assuming you are working with C# version 3, try this:

Assert.That( array, Is.All.Matches<int>( (x) => x%4 == 0 && x%100 != 0 || x%400 == 0 );

If it fails, it will give a generic message: "Expected: matching lambda expression" since NUnit is actually built with .NET 2.0, but for a quick test it may be just the tool you need.



It's the Tests » Blog Archive » Using Lambdas as Constraints in NUnit 2.5

view, Monday, August 19, 2019