![](/img/trans.png)
[英]NUnit Collection Assert That item in list is after/before another item
[英]Assert that a collection does not contain any item in another collection
考慮以下代碼:
string[] myCollection = { "a", "b", "c", "d" };
string[] anotherCollection = { "c", "d" };
我想測試myCollection
不包含anotherCollection
任何項目。 如何在NUnit中實現?
我嘗試了這些:
CollectionAssert.DoesNotContain(anotherCollection, myCollection);
Assert.That(myCollection, Has.No.Member(anotherCollection));
...但是問題是這些函數在單個實體上運行,我沒有找到任何使它們與數組一起工作的方法。
斷言集合不包含任何一組項目,我們可以檢查其他集合的交集是空的使用LINQ:
string[] subject = { "a", "b", "c", "d" };
string[] forbidden = { "c", "d" };
Assert.IsEmpty(subject.Intersect(forbidden));
// Or:
Assert.That(subject.Intersect(forbidden), Is.Empty);
上面顯示的測試將失敗,因為兩個集合都包含值"c"
和"d"
。
如果僅在集合包含所有禁止的項目時才希望測試失敗:
Assert.That(subject.Intersect(forbidden), Is.Not.EquivalentTo(forbidden));
// Or:
Assert.That(subject, Is.Not.SupersetOf(forbidden));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.