【问题标题】:NSubstitute: Match on any Dictionary with explicit args?NSubstitute:匹配任何带有显式参数的字典?
【发布时间】:2021-07-08 19:42:31
【问题描述】:

使用 NSubstitute,我如何匹配“任何”字典 - 只要它包含一组特定的键值对?

以下将匹配任何字典:

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

但我希望能够匹配任何字典只要它具有给定的键值对。例如,我想做这样的事情:

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

NSubstitute 中是否存在类似的东西?

【问题讨论】:

  • 使用如下: Dictionary dict = new Dictionary(); dict.Select(x => (x.Key == "abc") && (x.Value == "xyz"));
  • @jdweng -- 感谢您的回复,但是我不清楚如何将该逻辑融入到 NSubstitute 测试框架的 Received 调用中。澄清一下,我的要求不是关于代码中的过滤/匹配。这个问题专门与使用 NSubstitute 进行测试有关。如果我误解了,请纠正我。
  • NSubstitue 和 Linq 是应用程序的不同层。 NSubstitue 是一个类结构,而 Linq 用于执行查询。所以 NSubstitute 等价于名词,Linq 是动词

标签: c# unit-testing dictionary testing nsubstitute


【解决方案1】:

啊,原来错误是使用Arg.Any 而不是Arg.Is

这对我有用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多