I am trying to use FluentAssertions to combine collection and object graph comparison assertions.
I have the following class.
public class Contract
{
public Guid Id { get; set; }
public string Name { get; set; }
}
Which are returned in a collection, like so.
ICollection<Contract> contracts = factory.BuildContracts();
I then want to make sure that collection contains only specific Contract
objects.
contracts.Should().Contain(new Contract() { Id = id1, Name = "A" });
This doesn't work, I'm believe because Contain
is using object.Equals
rather than object graph comparison, (as provided by ShouldBeEquivalentTo
).
I also need to assert that the collection doesn't contain a specific object, i.e.
contracts.Should().NotContain(new Contract() { Id = id2, Name = "B" });
Effectively given a collection containing an unknown number of items, I want to ensure that; it contains a number of specific items, and that it doesn't contain a number of specific items.
Can this be achieved using the functions provided by FluentAssertions?
As a side note, I don't want to override object.Equals
for the reasons discussed here. Should I be using IEquatable to ease testing of factories?