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

Dictionary access incorrectly triggers CollectionShouldHaveElementAt suggestion #44

Closed
samcragg opened this issue Jun 16, 2018 · 2 comments · Fixed by #45
Closed

Dictionary access incorrectly triggers CollectionShouldHaveElementAt suggestion #44

samcragg opened this issue Jun 16, 2018 · 2 comments · Fixed by #45
Labels

Comments

@samcragg
Copy link
Contributor

I'm using FluentAssertions version 5.4.0 and FluentAssertions.Analyzers version 0.10.1. When I create a test that asserts the values in dictionaries, the analyzer incorrectly suggests I use HaveElementAt which doesn't compile, i.e. given this code:

[Fact]
public void DictionaryTest()
{
    var dictionary = new Dictionary<string, int> { ["123"] = 123 };
    dictionary["123"].Should().Be(123);
}

The analyzer incorrectly suggests:

[Fact]
public void DictionaryTest()
{
    var dictionary = new Dictionary<string, int> { ["123"] = 123 };
    dictionary.Should().HaveElementAt("123", 123);
}

Perhaps it could suggest (though it's not simplifying but is more idiomatic):

dictionary.Should().ContainKey("123").WhichValue.Should().Be(123);

Happy to take a stab at a PR

@jnyrup
Copy link
Member

jnyrup commented Jun 18, 2018

Just as a note.
Fluent Assertions also has a shorthand for

dictionary.Should().ContainKey(key).WhichValue.Should().Be(value);
dictionary.Should().Contain(key, value);

See https://fluentassertions.com/documentation/#dictionaries

@Meir017 Meir017 added the bug label Jun 18, 2018
@Meir017
Copy link
Member

Meir017 commented Jun 18, 2018

@samcragg I'll be happy to receive a pull-request for this 😄

I think the solution should be to make the CollectionShouldHaveElementAtAnalyzer class (or even CollectionAnalyzer) method ShouldAnalyzeVariableType be more strict and exclude Dictionary

  • make sure you run just the tests with the Completed category

Meir017 added a commit that referenced this issue Jun 24, 2018
CollectionShouldHaveElementAt_ShouldIgnoreDictionaryTypes should use all of the analyzers
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.

3 participants