In my current project we’re using Moq as our mocking framework for unit testing. It’s a very nice package – very simple, very intuitive for someone who has his wrapped around basic mocking concepts, but tonight I ran into an annoying limitation that, while rare, impeded my tests considerably.
Consider the following code:
1: public interface IOut2: {3: void DoOut (out string outval);4: }5:6: [Test]7: public void TestMethod()8: {9: var mock = new Mock(); 10: string outVal = "";11: mock.Setup(out => out.DoOut(out outVal))12: .Callback<string>(theValue => DoSomethingWithTheValue(theValue));13: }
What this code SHOULD do is create a mock object for the IOut interface that, when called with an out parameter, both assigns a value to it, and does something with the value in the Callback.
Unfortunately, this does not work.
The reason it doesn’t work is that the Callback method has many overloads, but they are all to various variants of the Action<> delegate. Either Action
“Invalid callback parameters <string> on object ISetup<string&>”
Note the highlighted bits – The Setup method referred to a string& (a ref/out param), while the Callback inferred an Action
So what CAN we do?
The first option is submit a patch to the Moq project. It’s open-source, and a solution might be appreciated, especially since the project’s lead, Daniel Cazzulino, pretty much acknowledged that this is not supported right now. I might do that later, but right now it’s the middle of the night and I just want my tests to pass. So I manage to hack together this workaround which does the trick:
1: public static class MoqExtension2: {3: public delegate void OutAction(out TOut outVal);
4: public static IReturnsThrows<TMock, TReturn>5: OutCallback<TOut, TMock, TReturn>6: (this ICallback<TMock,T4> mock,7: OutActionaction) 8: where TMock : class9: {10: mock.GetType()11: .Assembly.GetType("Moq.MethodCall")12: .InvokeMember("SetCallbackWithArguments",13: BindingFlags.InvokeMethod14: | BindingFlags.NonPublic15: | BindingFlags.Instance,16: null, mock, new object[] {action});17: return mock as IReturnsThrows<TMock,TReturn>;18: }19: }
We’ve created a new delegate, OutAction
Note and Caveats:
1) This code works for the very specific void Function (out T) signature. You will have to tailor it to the specific methods you want to mock. To make it generic, we’ll have to create a bucket-load of OutAction overloads, with different combinations of out and regular parameters.
2) This is very hacky and very fragile. If Moq’s inner implementation changes in a future version (the one I’m using is 4.0.10827.0), it will break.
3) I don’t like using out params, usually, but this is a 3rd party library, so I have to work with what I got.
Comments? Questions? Scathing criticisms on how ugly my code is? A different, simpler method of accomplishing this that I totally missed? Bring’em on.
