Idea for new collection assertion
See original GitHub issueHello, fluentassertions community,
I would like to propose a new overload of the BeEquivalentTo method for collection assertions. Here’s an example:
plannedOrders.Should().BeEquivalentTo(
order => order.Type == OrderType.Purchase && order.Quantity == 5 && order.Date == tomorrow,
order => order.Type == OrderType.Transfer && order.SourceLocation == distributionCenter && order.Quantity == 3 && order.Date == today,
order => order.Type == OrderType.Transfer && order.SourceLocation == factory && order.Quantity == 5 && order.Date == today.AddDays(10)
);
Basically, the API should be similar to the existing collection.Should().BeEquivalentTo() API but instead of providing a collection of elements as input we would instead provide a collection of predicates that elements of the collection must match. Assertion should pass if it is possible to have a 1-1 mapping between elements of the collection and the predicates.
The proposed API is useful in scenarios when the collection has elements with many properties but only few of these are insteresting in the scope of a given test scenario.
Could you please let me know if there is interest in the community in this feature? I would be happy to contribute.
Best regards, Vanya
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
I can only agree on your analysis.
I’m not sure if
BeEquivalentTo
is the best method to overload, as I would say that an item is not equivalent to a predicate, but matches or satisfies it. This is just my immediate gut feeling, which isn’t necessarily right, but just that I’m not (yet) convinced. Sometimes a proposed API just needs a bit of time to sink in. On the other hand we already haveContain(Expression<Func<T, bool>> predicate)
.The verbosity of
SatisfyRespectively
is to provide more detailed failure messages. An example from the tests:I have created a pull request with the suggested implementation. Looking forward to your comments. https://github.com/fluentassertions/fluentassertions/pull/1500