Expectations

Expectations are used to define the interaction between the object under test and mocks created with the NMock2 mockery.

You can define what you expect to be called on a dependency (method, property, indexer, event add/remove) as well as how the mock has to react on calls (return values, exceptions, other actions).

A simple expectation looks like this:

Expect.Once.On(mock).Method("Add").With(2, 3).Will(Return.Value(5));

This expectation defines that we expect that the method named Add is called exactly once on our mock with parameters 2 and 3 and that the mock should return 5 as method return value.

Expectation Syntax

The following diagram shows the complete syntax of NMock2:

Expectation Syntax

Expect

Expect is the starting point to define an expectation. You can us its static method to create expectation.

Cardinality

You can use one of the static methods/properties of the Expect class to define how many times a call on a mock has to occur.

Keyword Description
Once Exactly one time. Same as Exactly(1).
Exactly(int n) Exactly n times.
AtMost(int n) 0 to n times.
Between(int m, int n) Between m and n times.
AtLeastOnce At least one time.
AtLeast(n) At least n times.
Never A call should never occur. Normally this is the same as not defining an expectation at all. Sometimes it can help to exclude special cases overriding other expectations.

On

On(object o) is used to define on which mock to expect a call.

Method

The Method method is used to define the expected method. Method has several overloads:

Method(string name, params Type[] typeParams)
Method(Matcher nameMatcher, params Type[] typeParams)
Method(MethodInfo method, params Type[] typeParams
Method(Matcher methodMatcher, Matcher typeParamsMatcher

The method can be specified either by name, matcher or MethodInfo and for generic methods with type parameters.

If you do not specify type parameters then the method is only matched by name.

More details about matchers can be found on the page about Matchers.

With, WithNoArguments, WithAnyArguments

With is used to define which method arguments are expected.

You can either pass the really expected values to With (e.g. 2, "hello" or object instances) or Matchers (explaned later).

WithNoArguments can be used to destinguish a parameterless method from other overloads. If there are no overloads then you can skip WithNoArguments because the method will be matched correctly by Method already.

WithAnyArguments can be omitted because the expectation has the same meaning without it. You can add it for clarity.

You can omit to specify a With variant. This means that the method is matched independently of the method arguments passed in the test.

GetProperty

GetProperty(string name) is used to specify that a property has to be requested.

SetProperty

SetProperty(string name) is used to specify that a property has to be set.

To

To specifies the value that has to be set. You can either specify a real value or a matcher.

Set

You can use Set to specify an expectation on an indexer set operation.

Set is followed by the To statement which is explained above.

Get

Get specifies an expectation on an indexer get operation.

EventAdd

Use EventAdd to specify that your code under tests has to register an event of your mock.

EventRemove

Use EventRemove to specify that your code under tests has to unregister an event of your mock.

Matching

Matching is useful if you want to specify your one matcher that is used to check whether a call on a mock is the one that is specified by this expectation. See section Matcher for more details.

Matching is optional. If specified then the matching criterias defined by other syntax elements are combined with the passed matcher.

Will

Will defines the actions that are taken when this expectation matches a call on a mock. Actions can be setting of result values, throwing exceptions or any other code you provide.

See section Actions for more details.

Comment

If you want to add a message of your own to the expectation exception that is thrown when either something unexpected was called on a mock or an expectation was not met then you can use Comment. The string that you pass to it will be shown in the exception message.

We often use this to add a comment why a certain expectation has to be met in more complex unit tests.

Stub

Stub is just a short cut for Expect.AtLeast(0).


So that's it about expectations. On the next page you can see samples that explain all this a bit less abstract ;-)