Add equivalent to xUnit's Assert.Collection
See original GitHub issueDescription
xUnit has this:
Assert.Collection(things,
thing =>
{
thing.Id.Should().Be(guid1);
thing.Name.Should().Be("Thing1");
thing.Attributes.Should().NotBeNull();
thing.FullName.Should().MatchRegex("^Thing1 [^ ]+$");
},
thing =>
{
thing.Id.Should().Be(guid2);
thing.Name.Should().Be("Thing2");
thing.Attributes.Should().NotBeNull();
thing.FullName.Should().MatchRegex("^Thing2 [^ ]+$");
});
In some situations I find this style more expressive and/or succinct than the FluentAssertions methods that assert collection equality or equivalence.
Assert.Collection signature:
/// <summary>
/// Verifies that a collection contains exactly a given number of elements, which meet
/// the criteria provided by the element inspectors.
/// </summary>
/// <typeparam name="T">The type of the object to be verified</typeparam>
/// <param name="collection">The collection to be inspected</param>
/// <param name="elementInspectors">The element inspectors, which inspect each element in turn. The
/// total number of element inspectors must exactly match the number of elements in the collection.</param>
public static void Collection<T>(IEnumerable<T> collection, params Action<T>[] elementInspectors)
Proposed API
Per https://fluentassertions.slack.com/archives/C039Y2H7U/p1527880297000421
things.Should().SatisfyRespectively(thing => { ... }, thing => { ... });
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
xUnit : Assert two List<T> are equal?
xUnit.Net recognizes collections so you just need to do. Assert.Equal(expected, actual); // Order is important.
Read more >Testing list contents with xUnit and Assert.Collection | storck.io
Collection , verifies that the number of items in the list is equal to the number of inspectors and that all the assertions...
Read more >Add Assert.Equivalent · Issue #1604 · xunit/xunit
I think this is a great idea, an Assert.Equivalent has lots of use cases. However, I'm not sure if xUnit should be the...
Read more >XUnit - Assert.Collection
Collection expects a list of element inspectors, one for every item in the list ... Assert.Collection(list,. item => Assert.Equal(42, item),.
Read more >xUnit Assertions :: K-State CIS 400 Textbook
Collection () is a good choice when the collection is expected to always be in the same order, while the Assert.Contains() approach allows...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
On the surface having these two overloads looks good as they both work on
Action<T>
:(params Action<T>[] inspectors)
(IEnumerable<Action<T>> inspectors, string because, params string[] becauseArgs)
We already do that approach for several assertions:
ContainKeys
,NotContainKeys
,ContainValues
,NotContainValues
, and more.For some context/related issues, where
params
has caused issues.params object[]
overload forBeEquivalentTo
, #1036because
andbecauseArgs
parameters #806@jnyrup @dennisdoomen Can it be two overloads: one with
IEnumerable<Action<T>> inspectors
with because and args, and another one withparams Action<T>[] inspectors
without custom because?Another option is to use builder for specifying inspectors with single “because” at the end. Something like
Although it is slightly different mechanism especially because of specifying “Because” at the end…