Setting reference parameters in Moq

Tuesday 18 May 2010 at 12:18 pm. Used tags: , , , , , ,

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:

public class MockTester
{
	public string TestThis(ISomeThingToMock myMock) {
		StringBuilder s = new StringBuilder();
		myMock.MockWithRefParam(10, s);
		return s.ToString();
	}
}

Using NUnit as our testing framework together with Moq this is a quick example test to prove our theory:

using System.Text;
using NUnit.Framework;
using Moq;
using ReferenceParameterMoqExample;

namespace ReferenceParameterMoqExampleTests
{
	[TestFixture]
	public class SomeThingToMockTests
	{
		private MockTester _MockTester;
		private Mock<ISomeThingToMock> _SomeThingToMock;

		[Setup]
		public void TestSetup() {
			_MockTester = new MockTester();
			_SomeThingToMock = new Mock<ISomeThingToMock>();
		}

		[Test]
		public void TestThis_WithRefParam_ReturnsSpecificValue() {
			_SomeThingToMock
				.Setup(x => x.MockWithRefParam(It.IsAny<int>(), It.IsAny<StringBuilder>()))
				.Callback((int i, StringBuilder s) => s.Append("ThisWasMocked"));

			Assert.AreEqual("ThisWasMocked", _MockTester.TestThis(_SomeThingToMock.Object));
		}
	}
}

One comment

Leave a comment

Rob Wright

Just tried this out and it works sweeet. Good article.

Rob Wright - 19-05-’10 09:05
(optional field)
(optional field)
'cause I hate spambots, you gotta do this I'm afraid :\
Remember personal info?
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.

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
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…

Randolph Burt:

Intellisense helps me code faster and make less mistakes. However, from a personal point of view, it can be the difference between enjoying development and not enjoying development – and if you don’t …