如何指定模拟方法的集合参数应该只有一个元素

How do I specify that the collection parameter for a mocked method should have exactly one element

我正在为采用单个 IList<> 参数的方法设定预期。

如何在NMock3中表达如下语句:

Method XX of the mock should be called exactly once with a list object that contains exactly one item.

我想象的解决方案如下:

theMock.Expects.One.Method(_ =>_XX(null)).With(***mystery-mocking-goes-here***);

使用Is.Match:

theMock.Expects.One.Method(_ =>_XX(null)).With(Is.Match<IList<string>>(l => l.Count == 1));

Anantha Raju C 的解释

如果你有测试 _XX(T) 的方法。在 With 方法中,您必须传递一个 T 对象或一个匹配器。 Is.Match 创建它并需要一个谓词作为参数。

在此示例中,如果列表仅包含一项 (l.Count == 1),则谓词将 return 为真。