Setting reference parameters in Moq
Tuesday 18 May 2010 at 12:18 pm
A colleague just came to me asking for advice on how to set the value of a referenced parameter with Moq; after nearly berating him for not checking Google I was surprised to find examples out there don't actually make it that obvious to the uninitiated.
For the purposes of this quick post I've created a dummy interface and associated method to mock, and then a quick method to test the mocked interface. This might seem long-winded but it's standard measure in our team to ensure it's firmly covered.
public interface ISomeThingToMock
{
void MockWithRefParam(int someOtherParam, StringBuilder stringBuilder);
}Let's show how a class might implement the interface:
public class SomeThingToMock : ISomeThingToMock
{
public void MockWithRefParam(int someOtherParam, StringBuilder stringBuilder) {
stringBuilder.Append("ThisWasNotMocked");
}
}Now I'm going to create a quick method that uses the above so our example is obvious:
Read More