SelfReferencingCollectionAssertions.OnlyContain fails when collection is empty
See original GitHub issueDescription
SelfReferencingCollectionAssertions.OnlyContain
fails when asserting that an empty collection should contain only items matching a predicate, even though the asserted statement is vacuously true.
Complete minimal example reproducing the issue
// Arrange
var actual = new int[0];
// Assert
actual.All(item => true).Should().BeTrue();
actual.Should().OnlyContain(item => true);
Expected behavior:
Both assertions to pass.
Actual behavior:
The first assertion passes, but the second fails with message
Expected actual to contain only items matching True, but the collection is empty.
Versions
- Fluent Assertions: 5.10.3
- .NET runtime: .NET Core 3.1
Comments
This issue is similar in nature to my previous issue (whose resolution I’m grateful for). I would think that the behavior of OnlyContain
is also by design but that it might be more intuitive for the assertion to always pass on empty collections.
The perhaps counterintuitive behavior of OnlyContain
on an empty collection has been mentioned before (although only in passing).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (7 by maintainers)
Top Results From Across the Web
AllSatisfy() fails on empty collections #2143
Actual behavior. The test fails with exception. Expected collection to contain only items satisfying the inspector, but collection is empty. Regression?
Read more >c# - Check if collection is empty or not
You can use the Count field to check if the collection is empty or not. so you will end up with something like...
Read more >Tips - Fluent Assertions
now fails on empty collection; actual.Should().OnlyContain(x => x.SomeProperty). Expected True, but found False. Expected collection to contain only items ...
Read more >Collections
When asserting on a projection of a collection the failure message will be less descriptive as it only knows about the projected value...
Read more >Untitled
In the most simple form, you can fail a build by calling: Assert. ... `AllSatisfy()` fails on empty collections · fluentassertions .
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
You could compare it to
String.IsNullOrEmpty
, that would give something likeOnlyContainAndNotEmpty(predicate)
But in that case I think.NotBeEmpty().And.OnlyContain(predicate);
is just as good, if not better.The quote was to support your point of view - that even though it may be strictly true it can be misleading.