簡體   English   中英

斷言一個集合不包含另一個集合中的任何項目

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM