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.

Idea for new collection assertion

See original GitHub issue

Hello, 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:closed
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Jan 24, 2021

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 have Contain(Expression<Func<T, bool>> predicate).

The verbosity of SatisfyRespectively is to provide more detailed failure messages. An example from the tests:

public void When_asserting_collection_does_not_satisfy_any_inspector_it_should_throw()
{
    // Arrange
    var customers = new[]
    {
        new CustomerWithItems { Age = 21, Items = new[] { 1, 2 } },
        new CustomerWithItems { Age = 22, Items = new[] { 3 } }
    };

    // Act
    Action act = () => customers.Should().SatisfyRespectively(
        new Action<CustomerWithItems>[]
        {
            customer =>
            {
                customer.Age.Should().BeLessThan(21);
                customer.Items.Should().SatisfyRespectively(
                    item => item.Should().Be(2),
                    item => item.Should().Be(1));
            },
            customer =>
            {
                customer.Age.Should().BeLessThan(22);
                customer.Items.Should().SatisfyRespectively(item => item.Should().Be(2));
            }
        }, "because we want to test {0}", "nested assertions");

    // Assert
    act.Should().Throw<XunitException>().WithMessage(
@"Expected customers to satisfy all inspectors because we want to test nested assertions, but some inspectors are not satisfied:
*At index 0:
*Expected customer.Age to be less than 21, but found 21
*Expected customer.Items to satisfy all inspectors, but some inspectors are not satisfied:
*At index 0:
*Expected item to be 2, but found 1
*At index 1:
*Expected item to be 1, but found 2
*At index 1:
*Expected customer.Age to be less than 22, but found 22
*Expected customer.Items to satisfy all inspectors, but some inspectors are not satisfied:
*At index 0:
*Expected item to be 2, but found 3"
);
}
0reactions
vanya-lebedevcommented, Feb 27, 2021

I have created a pull request with the suggested implementation. Looking forward to your comments. https://github.com/fluentassertions/fluentassertions/pull/1500

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Simple Assertion - IntelliJ IDEA Guide
We can get IntelliJ IDEA to generate test methods for Spock specifications using ⌘N (macOS) or Alt+Insert (Windows/Linux) inside the class.
Read more >
Java assertions in IntelliJ IDEA Community?
Based on this answer for Eclipse you need to pass a JVM option -ea to enable assertions in any JVM. It's not unique...
Read more >
Custom Assertions in Java Tests - InfoQ
Each custom assertion written with AssertJ contains code which is responsible for the creation of an assertion object and the injection of the ......
Read more >
Is it a good idea to start a function with a bunch of assert ...
In general, it is a good idea to make your code easier to debug by adding extra tests, assertions and validations. Using a...
Read more >
What are the Different types of Asserts in Postman?
I hope you have got an idea of what assertions are and how to use them. You can combine the both Chai assertion...
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