Skip to content

.ContainKey().WithValue() and .Or/.And interact incorrectly #4670

Description

@ssrmm

Combining Or/And with the DictionaryContainsKeyValuePairConstraint produces incorrect results, see examples below.

The error messages in the failure cases imply that the expression is parsed in a weird/unintended way.

[Test]
public void BugReport()
{
    var dictionary = new Dictionary<string, int>
    {
        { "a", 123 },
        { "b", 456 }
    };

    Assert.Multiple(() =>
    {
        Assert.That(dictionary, Does.ContainKey("a").WithValue(456).Or.ContainKey("b").WithValue(123), "Passes but should fail");
        Assert.That(dictionary, Does.ContainKey("a").WithValue(456).And.ContainKey("b").WithValue(456), "Passes but should fail");

        // Expected: dictionary containing key "c" or dictionary containing entry ["c", 456]
        // But was:  < ["a", 123], ["b", 456] >
        Assert.That(dictionary, Does.ContainKey("a").WithValue(123).Or.ContainKey("c").WithValue(456), "Fails but should pass");

        // Expected: dictionary containing key "c" and dictionary containing entry ["c", 456]
        // But was:  < ["a", 123], ["b", 456] >
        Assert.That(dictionary, Does.ContainKey("a").WithValue(123).And.ContainKey("c").WithValue(456), "Fails but for the wrong reason");
    });
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions