Skip to content

MSTEST0037 incorrectly fixes non-generic Dictionary.Contains #9966

Closed

Description

@RobSwDev

Describe the bug

In the test class below, the analyzer fires on Assert.IsTrue(dict.Contains("a")), and the auto-fix modifies the behaviour of the test so it no longer passes.
This is because Dictionary.Contains seems to have different semantics from Dictionary<K,V>.Contains

[TestClass]
public class MSTEST0037
{
#pragma warning disable MSTEST0037 // Use proper 'Assert' methods
    [TestMethod]
    public void Example1_DictionaryCount()
    {
        var dict = CreateStringStringDictionaryTypedAsNonGeneric();
        Assert.IsTrue(dict.Contains("a"), "Expected dictionary to contain key 'a'");
    }
#pragma warning restore MSTEST0037 // Use proper 'Assert' methods

    /// <summary>
    /// This is the the result of applying MSTest0037 auto-fix to test <see cref="Example1_DictionaryCount"/>
    /// It makes the test fail.
    /// </summary>
    [TestMethod]
    public void Example1_DictionaryCount_FixedByMsTest0037()
    {
        var dict = CreateStringStringDictionaryTypedAsNonGeneric();
        Assert.Contains("a", dict, "Expected dictionary to contain key 'a'");
    }

    private static IDictionary CreateStringStringDictionaryTypedAsNonGeneric()
        => new Dictionary<string, string>() { { "a", "b" } };
}

Version used

Latest:
MSTest.TestAdapter & TestFramework: 4.3.2
Microsoft.NET.Test.Sdk: 18.8.1
Microsoft.Testing.Platform: 2.3.2

Steps To Reproduce

See repro above

Expected behavior

Fixes should not change behaviour

Actual behavior

The fix changes behaviour (and the test fails)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions