2017-08-02 39 views
-3

我有試驗這顯示以下xUnit.net輸出:爲什麼是試驗失敗的HashSet的比較,其中元素的順序似乎無關緊要

[xUnit.net 00:00:07.1166826]  Expected: HashSet<License> [Comp.Licensing.Web.Model.License [5d8104ef-f707-4a40-9d68-463bf9f8b0f9], Comp.Licensing.Web.Model.License [d586fc23-bba6-474c-82a2-226484d7fb81]] 
[xUnit.net 00:00:07.1172482]  Actual: HashSet<License> [Comp.Licensing.Web.Model.License [d586fc23-bba6-474c-82a2-226484d7fb81], Comp.Licensing.Web.Model.License [5d8104ef-f707-4a40-9d68-463bf9f8b0f9]] 

我不明白的是爲什麼這個測試失敗,看起來原因是HashSet失靈。

具體來說,它看起來實際是完全相同的**預期。

實際:

HashSet<L> [Stuff [abc], Stuff [123]] 

預計:

HashSet<L> [Stuff [123], Stuff [abc]] 

DOC上爲HashSet,它說

一組是不包含重複元素的集合,其元素沒有特定的順序。

我的測試是否由不使用相同的相等檢查HashSet的東西運行?

+2

如何發佈您的測試,以便我們能夠看到什麼可能是錯誤的,而不是試圖去猜測... –

+1

你檢查兩個HashSet對象的引用是同一個對象,或兩個HashSet的對象具有相同的內容?你測試什麼樣的平等? – hatchet

+0

對不起@ L.B,我會編輯解釋我的困惑。我不明白的是**消息中的** Expected ** HashSet與** Actual **相同​​* – r12

回答

2

HashSet不覆蓋Equals,並將使用object.Equals,這是參考相等。

HashSet不保證元素的順序。

如果您使用的是Assert.Equal,那麼集合的順序很重要。

改爲使用CollectionAssert.AreEquivalent

來源:xUnit : Assert two List<T> are equal?

相關問題