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.

SelfReferencingCollectionAssertions.OnlyContain fails when collection is empty

See original GitHub issue

Description

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

github_iconTop GitHub Comments

2reactions
eNeRGy164commented, Feb 8, 2021

You could compare it to String.IsNullOrEmpty, that would give something like OnlyContainAndNotEmpty(predicate) But in that case I think .NotBeEmpty().And.OnlyContain(predicate); is just as good, if not better.

1reaction
jnyrupcommented, Feb 8, 2021

The quote was to support your point of view - that even though it may be strictly true it can be misleading.

Read more comments on GitHub >

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

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