question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add equivalent to xUnit's Assert.Collection

See original GitHub issue

Description

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:closed
  • Created 5 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, May 6, 2019

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 for BeEquivalentTo, #1036
  • Not having because and becauseArgs parameters #806
2reactions
ishimkocommented, May 6, 2019

@jnyrup @dennisdoomen Can it be two overloads: one with IEnumerable<Action<T>> inspectors with because and args, and another one with params Action<T>[] inspectors without custom because?

Another option is to use builder for specifying inspectors with single “because” at the end. Something like

things.Should().SatisfyRespectively()
   .Thing(first => { ... })
   .Thing(second => { ... })
   .Becasue("there is such a feature in xUnit");

Although it is slightly different mechanism especially because of specifying “Because” at the end…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found