Actions are passed to With(params IAction[] actions) in the expectation definition. These actions are executed when the expectation is met.

Actions can be used set return values, throwing exceptions or anything else you want to happen when the expectation is met.

NMock2 Action Shortcuts

NMock2 provides some static classes that you can use to create commonly used actions:


The Return class provides static methods for the most important scenarios (shortcuts to action creation):


You can define the return value of a method, property getter or indexer getter with Return.Value(object value)

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

Will(Return.Value(obj)) is the same as Will(new ReturnAction(obj)).

Return.CloneOf(IClonable prototyp)

Instead of defining a value to return, you can specify that a clone of the specified prototyp should be returned.

Will(Return.CloneOf(obj)) is the same as Will(new ReturnCloneAction(prototype))


Return.OutValue(string parameterName, object value) and Return.OutValue(int parameterIndex, object value) can be used to define values of out parameters of methods - either by name or index of the out parameter.

Will(Return.OutValue(parameterName, value) is the same as Will(new SetNamedParameterAction(parameterName, value))

Will(Return.OutValue(index, value) is the same as Will(new SetIndexedParameterAction(index, value))


Throw.Exception can be used to specify that an exception has to be thrown when the expectation is met. This way you can easily test how your code reacts when method calls to dependencies throw excpetions:

Will(Exception(System.Exception exception)) is the same as Will(new ThrowAction(exception))


The Signal class provides a static method to create a SignalAction that can be used to synchronize threads in multi threaded unit tests. For example to synchronize the worker thread of your code under test with your unit test code.

Multiple Actions

You can define multiple actions that are executed when an expectation is met. For example you can define a return value, set several out parameters, signal an event wait handle and execute your custom actions:

    Return.OutValue("msg", "hello"), 
    new MyCustomAction())

Custom Actions

You can implement your own actions. The sample below is the CollectAction (included in NMock2) that is used to collect an argument passed to a mocked method:

/// <summary>
/// Action that returns the n-th element of the arguments to an invocation.
/// </summary>
public class CollectAction : IAction
    private readonly int argumentIndex;
    private object collectedArgumentValue = null;

    /// <summary>
    /// Initializes a new instance of the <see cref="CollectAction"/> class.
    /// </summary>
    /// <param name="argumentIndex">Index of the argument to collect.</param>
    public CollectAction(int argumentIndex)
        this.argumentIndex = argumentIndex;

    /// <summary>
    /// Invokes this action. 
    /// This method is called when an expectation containing this action is met.
    /// </summary>
    /// <param name="invocation">The invocation.</param>
    public void Invoke(Invocation invocation)
        collectedArgumentValue = invocation.Parameters[argumentIndex];

    /// <summary>
    /// Describes this object.
    /// </summary>
    /// <param name="writer">The text writer the description is added to.</param>
    public void DescribeTo(TextWriter writer)
        writer.Write("collect argument at index ");

    /// <summary>
    /// Gets the collected parameter.
    /// </summary>
    /// <value>The collected parameter (n-th parameter of parameter list of the method's call.</value>
    public object Parameter
        get { return collectedArgumentValue; }