接上一篇:
7、Collection Constraint
集合结束为指定的集合执行测试,主要的语法有:
Is.All...,Has.All... 为集合中的每一个元素申请一个约束,所有元素符合要求则测试成功。
Has.Some... 为集合中的每一个元素申请约束,最少有一个符合要求则测试成功。
Has.None... 所有的元素都不都不符合要求则测试成功。
Is.Unique 测试集合拥有一个唯一值
Has.Member(object) 测试集合拥有一个对象
Is.EquivalentTo(IEnumerable) 测试两个集合相等,需要拥有相同的items.
Is.SubsetOf(IEnumerable) 测试一个集合是否是另一个的子集
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That( iarray, Is.All.Not.Null );
Assert.That( sarray, Is.All.InstanceOfType(typeof(string)) );
Assert.That( iarray, Is.All.GreaterThan(0) );
Assert.That( sarray, Is.Unique );
Assert.That( iarray, List.Contains(3) );
Assert.That( sarray, List.Contains("b") );
Assert.That( sarray, List.Not.Contains("x") );
Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) );
Assert.That( new int[] { 1, 2, 2 }, Is.Not.EquivalentTo( iarray ) );
Assert.That( new int[] { 1, 3 }, Is.SubsetOf( iarray ) );
// Using inheritance
Expect( iarray, All.GreaterThan( 0 ) );
Expect( sarray, Contains("b") );
Expect( new int[] { 1, 3 }, SubsetOf( iarray ) );
8、Property Constraint
测试对象的属性。Has.Property(string) //测试指定对象是否存在
Has.Property(string,object) //测试对象属性值是否与提供的值相等
Has.Lenght(int) // 测试对象的长度是否与指定的值相等
Has.Count(int) //测试对象的总数是否与指定的值相等
ArrayList arrayList1 = new ArrayList();
arrayList1.Add("ccnu");
arrayList1.Add("whu");
arrayList1.Add("hsut");
int[] array = new int[8] {0,1,2,3,4,5,6,7 };
NUnit.Framework.Assert.That(array,Has.Property ("Length"));
NUnit.Framework.Assert.That(array,Has.Property ("Length").EqualTo(8));
NUnit.Framework.Assert.That(array,Has.Length (8));
//Array has not Property Count
//NUnit.Framework.Assert.That(array,Has.Count (8)); // Fail
NUnit.Framework.Assert.That(arrayList1,Has .Property ("Count"));
NUnit.Framework.Assert.That(arrayList1,Has.Count(3));