C# 在NUnit 2.5中使用Lambdas作为约束?
根据,可以在NUnit 2.5中使用Lambda表达式作为约束。我就是不能让它工作?我正在使用NUnit 2.5.3.9345 使用博客文章中的示例lambda:C# 在NUnit 2.5中使用Lambdas作为约束?,c#,unit-testing,nunit,lambda,C#,Unit Testing,Nunit,Lambda,根据,可以在NUnit 2.5中使用Lambda表达式作为约束。我就是不能让它工作?我正在使用NUnit 2.5.3.9345 使用博客文章中的示例lambda: [TestFixture] public class Class1 { [Test] public void someTest() { int[] array = {1, 2, 3, 4}; Assert.That( array, Is.All.Matches( (x) =>
[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 ));
}
}
结果在编译器中显示:
“无法将lambda表达式转换为类型‘NUnit.Framework.Constraints.Constraint’,因为它不是委托类型”
程序集的目标框架是.NET Framework 3.5。有什么我愚蠢地做错了吗?我认为编译器无法处理lambda,因为它无法推断参数类型。 试试这个:
Assert.That( array, Is.All.Matches( (int x) => x%4 == 0 && x%100 != 0 || x%400 == 0 ));
匹配
约束在我使用的NUnit版本(2.5.9)中有3个重载,其中一个是
public Constraint Matches<T>(Predicate<T> predicate)
公共约束匹配(谓词)
因此,如果在方法调用中传入类型参数,可能会起作用,如下所示:
Assert.That(array, Is.All.Matches<int>(x => (rest of lambda body)));
Assert.That(数组,Is.All.Matches(x=>(lambda体的其余部分));
可以使用Has.All.Matches(somepredicate)对集合定义约束,并使用NUnit Framework 2.6.12296版进行测试
[测试]
[测试用例(“1000”)]
public void listsuborganizationsfiltersawaydepreactedorganizations(字符串pasId)
{
var request=ListOrganizations2GRequest.Initialize(pasId);
var unitsnotfilter=OrganizationWSAgent.ListOrganizations2G(PasSystemTestProvider.passystemWhenTest,请求);
request.ValidPeriod=new ListOrganizations2GrestValidPeriod{ValidFrom=new DateTime(2015,3,24),ValidFromSpecified=true};
var unitsFiltered=OrganizationWSAgent.ListOrganizations2G(PasSystemTestProvider.passystemWhentest,请求);
Assert.IsNotNull(unitsNotFiltered);
Assert.IsNotNull(unitsFiltered);
CollectionAssert.IsNotEmpty(unitsFiltered.Organization);
CollectionAssert.IsNotEmpty(unitsNotFiltered.Organization);
int[]unitidFiltered=unitsFiltered.Organization[0]。subdiaryorganization.Select(so=>so.Id).ToArray();
var filteredUnits=unitsNotFiltered.Organization[0]。子日记组织
.Where(u=>!unitIdsFiltered.Contains(u.Id)).ToList();
Assert.IsNotNull(filteredUnits);
CollectionAssert.IsNotEmpty(filteredUnits);
Assert.That(filteredUnits,Has.All.Matches(ohs=>(!IsValidPeriodForToday(ohs)));
}
私有静态布尔值今天有效(OrganizationHierarchySimpleType ohs)
{
返回ohs.ValidPeriod!=null
&&ohs.ValidPeriod.ValidFrom=DateTime.Now;
}
按其应有的方式工作。有点惭愧,我自己没有注意到:(我也有同样的问题,看起来我只是没有正确地理解lambda语法。谢谢!:)如果数组项是匿名对象呢?
[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;
}