Skip to content

Misleading diagnostic and wrong fix for array.Length.Should().Be() #242

Closed
@petterh

Description

@petterh

Description

Misleading diagnostic and wrong fix when asserting array length

Complete minimal example reproducing the issue

int[] ar = new int[2];
ar.Length.Should().Be(2);

Expected behavior:

I'm not sure what to expect. "Actual behavior" should make the problem clear.

Actual behavior:

The code triggers FluentAssertions0206: Use .Should().HaveLength() instead.
This would be fine, except the only HaveLength I've seen applies to strings, not to arrays.

Applying the "Simplify Assertion" fix changes the code to:

ar.Should().HaveLength(2);

This does not compile, as ar is not a string.

If FluentAssertions were to support array Length properties with a HaveLength assertion, that would be nice.
In lieu of that, HaveCount would actually make sense here.

Versions

FluentAssertions 6.9.0
FluentAssertions.Analyzers 0.25.0
target .net6.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions