我想使用FluentAssertions結合集合和對象圖比較斷言。FluentAssertions;結合集合和對象圖比較斷言
我有以下類。
public class Contract
{
public Guid Id { get; set; }
public string Name { get; set; }
}
哪些返回集合中,就像這樣。
ICollection<Contract> contracts = factory.BuildContracts();
我再想確保集合只包含特定Contract
對象。
contracts.Should().Contain(new Contract() { Id = id1, Name = "A" });
這是不行的,我相信,因爲Contain
使用object.Equals
而不是對象圖相比,(由ShouldBeEquivalentTo
提供)。
我還需要斷言集合不包含特定對象,即
contracts.Should().NotContain(new Contract() { Id = id2, Name = "B" });
有效送達包含未知數量的項目的集合,我要保證;它包含許多特定的項目,並且它不包含許多特定的項目。
這可以使用FluentAssertions提供的函數來實現嗎?
作爲一個方面說明,我不想覆蓋object.Equals
由於這裏討論的原因。 Should I be using IEquatable to ease testing of factories?
+1並感謝您的建議。然而,這不是我想要的答案,我用來覆蓋equals,只是使用標準的'Assert.IsTrue(contracts.Contains(...))'。但是這有它自己的問題(http://stackoverflow.com/questions/33988487/should-i-be-using-iequatable-to-ease-testing-of-factories)。我希望使用FluentAssertions來避免重寫Equals。 –
有趣的鏈接...我相信你會弄清楚什麼。我會密切關注帖子,看看它是如何演變的:)。順便說一句,你有沒有給「[應該](https://github.com/shouldly/shouldly)」跑步?我不確定它的默認行爲是什麼,但它可能以您想要的方式工作。 – Noctis