Skip to content

AllSatisfy() fails on empty collections #2169

@dennisdoomen

Description

@dennisdoomen

Discussed in #2143

Originally posted by ArneMancofi March 8, 2023

Description

new object[0].Should().AllSatisfy(o => { }); fails with

Expected collection to contain only items satisfying the inspector, but collection is empty.

This does not implement the documented effect of AllSatisfy:

Asserts that a collection contains only items which meet the criteria provided by the inspector

In particular, any empty collection should always pass this test.

The bug is probably due to two code lines in

.ForCondition(subject => subject.Any())
.FailWith("but collection is empty.")

Reproduction Steps

// Arrange
var emptyArray = new object[0];
// Act
// Assert
emptyArray.Should().AllSatisfy(o => { });

Expected behavior

The test should pass without throwing an exception.

Actual behavior

The test fails with exception

Expected collection to contain only items satisfying the inspector, but collection is empty.

Regression?

This has probably always been this way.

Known Workarounds

Code like this would be a workaround:

if (collection.Any()) collection.Should().AllSatisfy(...);

However, reading the documentation of .AllSatisfy(), it should not be necessary.

Configuration

FluentAssertions 6.9.0
.NET 6.0

Other information

The WikiPedia article on universal quantification states that

By convention, the formula ∀x∈∅ P(x) is always true, regardless of the formula P(x)

so it is the documentated behaviour of .AllSatisfy() that is conventionally correct, and the implementation that is not.

Metadata

Metadata

Assignees

Labels

api-suggestionEarly API idea and discussion, it is NOT ready for implementationbreaking change

Type

No type

Projects

Status

✅ Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions