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

Reading list

  • Josh Arnold - Web developer, author & contributor of FubuMVC
  • Jeremy D Miller - Web developer, author & contributor of StructureMap & FubuMVC
  • Roy Osherove - Unit Testing, Agile Development, Leadership & .NET
  • Rob Ashton - Technical Lead & author of MvcEx and AutoPoco
Randolph Burt:

Very interesting blog post – and one where I find myself saying “I agree” as I read each sentence. The other day I was thinking how one’s life journey takes different twists and turns as you interact …

Jon:

Wow! Can I work for you?! ** Breath of fresh air in management! **

Ian:

There will have to be very strict guidelines, the last thing you want is dispute over “.. but you gave him 5 credits and her only 3?!” which will undoubtedly lead to the scheme being rescinded. That re…

Gary Ewan Park:

This is a very interesting idea! It does leave some questions about how it is “policed”, and who decides whether a particular blog post, or SO answer warrants the credits, but I really do think that i…

Chris Marisic:

Being both a developer and a leader of developers, I have found little to be as integral to the speed and success of software development as intellisense. This is also why I leverage R# on top of Vis…

Franc:

I have to admit that Spark won me over very quickly, yes it is not perfect and yes I am not an expert in the subject but it has provided me with new skills. I have been a victim of its lack of intellis…