1

I am new to NMock2 and Moq frameworks. I need some help converting NMock2 code in my current project to Moq code:

var myMock= myMockery.NewMock<IMyInterface>();
var myRoleMock = myMockery.NewMock<IRoleInterface>();
var acceptAction = new MyAcceptAction(myRoleMock);
Stub.On(myMock).Method("Accept").Will(acceptAction);

I am also not clear what Will() in the above code stands for. I do have an idea that Will(Return.Value(something)) in NMock2 is equivalent to Returns(something) in Moq.

So are Will(something) and Will(Return.Value(something)) the same?

k.m
  • 30,794
  • 10
  • 62
  • 86
Adrian T
  • 33
  • 7

1 Answers1

1

To know the difference we have to investigate a little. Let's take a look under the hood, Will method's signature stands as:

public void Will(params IAction[] actions)

Now, the IAction interface derives from one particulary important interface which is IInvokable. As name implies, this allows implementors to be invoked (via Invoke method). How does it relate to Return.Value?

public class Return
{
    public static IAction Value(object result)
    { 
        return new ReturnAction(result);
    }
}

Digging a little bit deeper we can find that ReturnAction in its Invoke method simply sets return value to be used/expected by mock. That's however not the point. Return.Value is a simple wrapper creating IAction required by Will.

Your MyAcceptAction also has to implement this interface; to know what exactly happens in the

Stub.On(myMock).Method("Accept").Will(acceptAction);

line, you will have to check how MyAcceptAction is implemented. In particular, the Invoke method.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • thnks a lot jimmy.:) .. Just a small doubt ... So the usage of the invoke method in the implementation of the IAction or IInvokable interface( in this case, MyAcceptAction class) will be associated only with the Will(something) method? If tat is the case, I will b able to use this Interface or invoke method only for NMock2 and not for Moq? .. – Adrian T Jun 22 '12 at 10:46
  • @NiranjanK: Either with `Will` or with NMock itself. `IInovakable` is NMock API, so whatever is happening in `Invoke` method, you'd have to *"translate"* to Moq code. – k.m Jun 22 '12 at 12:19
  • Thanks a lot..:) was able to do it with the help of a Callback in Moq. – Adrian T Jun 25 '12 at 04:19