Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
petterh opened this issue Oct 27, 2023 · 2 comments · Fixed by #243
Closed

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

petterh opened this issue Oct 27, 2023 · 2 comments · Fixed by #243
Assignees
Labels

Comments

@petterh
Copy link

petterh commented Oct 27, 2023

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
@Meir017
Copy link
Member

Meir017 commented Oct 28, 2023

@petterh fixed in release 0.26.0, thanks for reporting this!

@petterh
Copy link
Author

petterh commented Oct 30, 2023

@Meir017 Thanks for fixing -- impressive turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants