views:

110

answers:

1

According to Charlie Poole's NUnit blog, it is possible to use Lambda expressions as constraints in NUnit 2.5. I just can't seem to be able to get it to work? I am using NUnit 2.5.3.9345.

Using the example lambda from the blog post:

[TestFixture]
public class Class1
{
    [Test]
    public void someTest()
    {
        int[] array = {1, 2, 3, 4};
        Assert.That( array, Is.All.Matches( (x) => x%4 == 0 && x%100 != 0 || x%400 == 0 ));
    }
}

Results in the compiler saying: "Cannot convert lambda expression to type 'NUnit.Framework.Constraints.Constraint' because it is not a delegate type"

The Target Framework of the assembly is .NET Framework 3.5. Is there something I'm stupidly doing wrong?

+2  A: 

I think the compiler can't deal with the lambda because it can't infer the parameter type. Try this :

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

Hope it helps !

Cédric

Cédric Rup
That works as it should. Slightly ashamed I didn't notice that myself :(
sgrassie
The compiler message wasn't particularly explicit...
Cédric Rup