Skip to content

SelfReferencingCollectionAssertions.OnlyContain fails when collection is empty #1492

@Overlong

Description

@Overlong

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions