Visitar URL original
BeEmpty for IEnumerable<T> assertions now lists the first 10 items in… by Xceed-DelvaJB · Pull Request #3198 · fluentassertions/fluentassertions · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class GenericCollectionAssertions<TCollection, T, TAssertions> : Referenc
where TCollection : IEnumerable<T>
where TAssertions : GenericCollectionAssertions<TCollection, T, TAssertions>
{
private const int MaxItemsShownOnBeEmptyFailure = 10;
private readonly AssertionChain assertionChain;

public GenericCollectionAssertions(TCollection actualValue, AssertionChain assertionChain)
Expand Down Expand Up @@ -285,17 +286,17 @@ public AndConstraint<TAssertions> AllBeOfType(Type expectedType, [StringSyntax("
/// </param>
public AndConstraint<TAssertions> BeEmpty([StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
{
var singleItemArray = Subject?.Take(1).ToArray();
var firstItems = Subject?.Take(MaxItemsShownOnBeEmptyFailure).ToArray();

assertionChain
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to be empty{reason}, ", chain => chain
.Given(() => singleItemArray)
.Given(() => firstItems)
.ForCondition(subject => subject is not null)
.FailWith("but found <null>.")
.Then
.ForCondition(subject => subject.Length == 0)
.FailWith("but found at least one item {0}.", singleItemArray));
.FailWith("but found at least these items {0}.", firstItems));

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void When_collection_is_not_empty_unexpectedly_it_should_throw()

// Assert
act.Should().Throw<XunitException>()
.WithMessage("*to be empty because that's what we expect, but found at least one item*1*");
.WithMessage("*to be empty because that's what we expect, but found at least these items {1, 2, 3}.");
}

[Fact]
Expand Down Expand Up @@ -130,7 +130,22 @@ public void When_asserting_non_empty_collection_is_empty_it_should_enumerate_onl

// Assert
act.Should().Throw<XunitException>()
.WithMessage("*to be empty, but found at least one item {1}.");
.WithMessage("*to be empty, but found at least these items {1, 2, 3}.");
collection.GetEnumeratorCallCount.Should().Be(1);
}

[Fact]
public void When_asserting_non_empty_collection_is_empty_it_should_take_up_to_ten_elements()
{
// Arrange
var collection = new CountingGenericEnumerable<int>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);

// Act
Action act = () => collection.Should().BeEmpty();

// Assert
act.Should().Throw<XunitException>()
.WithMessage("*to be empty, but found at least these items {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.");
collection.GetEnumeratorCallCount.Should().Be(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void When_the_collection_is_not_empty_unexpectedly_it_should_throw()
act
.Should().Throw<XunitException>()
.WithMessage(
"Expected collection to be empty because we want to test the failure message, but found at least one item*one*");
"Expected collection to be empty because we want to test the failure message, but found at least these items {\"one\", \"two\", \"three\"}.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Should_fail_with_descriptive_message_when_asserting_dictionary_with_
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected dictionary to be empty because we want to test the failure message, but found at least one item {[1, One]}.");
"Expected dictionary to be empty because we want to test the failure message, but found at least these items {[1, One]}.");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void When_a_derived_class_has_longer_formatting_than_the_base_class()
act.Should().Throw<XunitException>()
.WithMessage(
"""
Expected subject to be empty, but found at least one item {
Expected subject to be empty, but found at least these items {
FluentAssertions.Specs.Primitives.Complex
{
Statement = "goodbye"
}
}, Simple(Hello)
}.
""");
}
Expand Down
5 changes: 5 additions & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ sidebar:
nav: "sidebar"
---

## Unreleased

### Enhancements
* `BeEmpty` for `IEnumerable<T>` assertions now lists the first 10 items in the collection instead of only the first one - [#3198](https://github.com/fluentassertions/fluentassertions/pull/3198)

## 8.9.0

### What's new
Expand Down
Loading