Search code examples
nmock

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


I am setting expectations for a method that takes a single IList<> parameter.

How do I express in NMock3 the following statement:

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

The solution I imagine would be something like the following:

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

Solution

  • Use Is.Match:

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

    Explanation for Anantha Raju C

    If you have a method to test _XX(T). In the With method you have to pass a T object or a matcher. Is.Match create it and need a Predicate as argument.

    In this example, the Predicate will return true if the list contains only one item (l.Count == 1).