简体   繁体   中英

Using Lambdas as Constraints in NUnit 2.5?

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?

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 ));

The Matches constraint has 3 overloads in the version of NUnit I'm using (2.5.9), one of which is

public Constraint Matches<T>(Predicate<T> predicate)

So if you pass in the type parameter in the method call, that might work, like this:

Assert.That(array, Is.All.Matches<int>(x => (rest of lambda body)));

It is possible to define a constraint on a collection, testing with NUnit Framework version 2.6.12296, using the Has.All.Matches(somepredicate) .

    [Test]
    [TestCase("1000")]
    public void ListSubOrganizationsFiltersAwayDeprecatedOrganizations(string pasId)
    {
        var request = ListOrganizations2GRequest.Initialize(pasId);

        var unitsNotFiltered = OrganizationWSAgent.ListOrganizations2G(PasSystemTestProvider.PasSystemWhenTesting, request);

        request.ValidPeriod = new ListOrganizations2GRequestValidPeriod { ValidFrom = new DateTime(2015, 3, 24), ValidFromSpecified = true };

        var unitsFiltered = OrganizationWSAgent.ListOrganizations2G(PasSystemTestProvider.PasSystemWhenTesting, request);

        Assert.IsNotNull(unitsNotFiltered);
        Assert.IsNotNull(unitsFiltered);
        CollectionAssert.IsNotEmpty(unitsFiltered.Organization);
        CollectionAssert.IsNotEmpty(unitsNotFiltered.Organization);

        int[] unitIdsFiltered = unitsFiltered.Organization[0].SubsidiaryOrganization.Select(so => so.Id).ToArray();

        var filteredUnits = unitsNotFiltered.Organization[0].SubsidiaryOrganization
            .Where(u => !unitIdsFiltered.Contains(u.Id)).ToList();

        Assert.IsNotNull(filteredUnits);
        CollectionAssert.IsNotEmpty(filteredUnits);
        Assert.That(filteredUnits, Has.All.Matches<OrganizationHierarchySimpleType>(ohs => (!IsValidPeriodForToday(ohs)))); 
    }

    private static bool IsValidPeriodForToday(OrganizationHierarchySimpleType ohs)
    {
        return ohs.ValidPeriod != null
               && ohs.ValidPeriod.ValidFrom <= DateTime.Now && ohs.ValidPeriod.ValidTo >= DateTime.Now;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM