Search code examples
c#tddunit-testingnmock

mocking method with an array argument which will change after call


I have an issue with mocking method call with reference argument (byte[]) that will change after the call:

private Mock<ISocket> mSocket;    
byte[] buffer = new byte[1000];
mSocket.Expects.One.Method(x => x.Receive(null))
.With(buffer).WillReturn(2);

but after the call, I need to have buffer data changed.

buffer[0] = 10;
buffer[1] = 20;

How can I mock this behavior without changing production code?


Solution

  • You can use the method .Will chained onto your .WillReturn() to call a custom action.

    For example you can call .Will(UpdateBuffer())

    The UpdateBuffer method can then set the values you want in the buffer. UpdateBuffer is a method on a class that implements the IAction interface.

    Have a look at the Actions section of this nmock page, its all explained there and there is no need for me to repeat it.