Matchers are used to find a matching expectation when a call to a mock is invoked. The mockery searches through its expectation until it finds one for that all specified matchers match.
An example:
Expect.Once.On(mock).Method("Add").With(2, 3);
If your code under test calls the mock
this expectation matches if and only if the method Add
is called and 2
and 3
are passed as arguments. That means that internally there exist three matchers:
a matcher for the method name, one for the first first argument and one for the second argument.
You can play around with matchers, and try something like this:
Expect.Once.On(mock).Method( new StringContainsMatcher("dd"), new GenericMethodTypeParametersMatcher(typeof(decimal)).With( new ElementMatcher(1, 2, 3), new ToStringMatcher("3"));
This will result is an expectation that matches for generic methods that contain a dd
in its name and decimal
as type parameter and the first method argument as to be one out of 1, 2 or 3 and the second argument has to have 3 as its string representation.
This is not the way you should define expectations but I hope you get a clue what is possible with matchers.
If you have a situation in which the possibilities with the normal NMock2 syntax elements (like Method, GetProperty, ...) are not sufficient
then you can use the Matching
syntax element and pass your custom matcher to it that will match the desired invocation on the mock.
See Expectation Samples or acceptance test of NMock2 for samples.