Search code examples
c#unit-testingdictionarytestingnsubstitute

NSubstitute: Match on any Dictionary with explicit args?


With NSubstitute, how can I match on 'any' Dictionary - so long as it contains a certain set of key-value pairs?

The following will match any Dictionary:

mockObject.Received().Method(Arg.Any<Dictionary<string, string>>());

But I'd like to be able to match any Dictionary so long as it has the given key-value pairs. For example, I'd like to do something like:

mockObject.Received().Method(Arg.Any<Dictionary<string, string>> { {"MyKey": "MyValue"} });

Does something like that exist in NSubstitute?


Solution

  • Ah, turns out the mistake was using Arg.Any rather than Arg.Is.

    This worked for me:

    mockObject.Received().Method(Arg.Is<Dictionary<string, string>>(x => x.ContainsKey("MyKey") && x["MyKey"] == "MyValue"));