I am trying to test private methods in a Unit test project. So far, everything is going fine, but I hit a beat when I have to test a method with an out parameter. Signature for this method:
private bool GotSSI(out SSI ssi, RSI rsi) { ~code omitted~ }
And unittest (the part that doesn't work) looks like this:
SSI ssi = null; object[] p = new object[]{ssi,rsi}; Type[] t = new Type[] { typeof(SSI).MakeByRefType(), typeof(RSI) }; actual = (bool) privateTarget.Invoke("GotSSI",t,p);
The GotSSI method works. I tested it in debug mode in Unit test, and I see that the 'ssi' out variable is set inside the method before returning true or false. But when the test returns its own code, the 'ssi' variable is still zero. Thus, the problem is that the object that I created in the "GotSSI" method does not understand the method of calling PrivateObject.
Does anyone know what I'm missing?
Update (solution from Rafal)
Rafal's solution works great, and here's how I implemented the solution.
I created a delegate:
delegate bool GotSSIInternal(out SSI ssi, RSI rsi);
And when I created the object that I wanted to test, I create a delegate (the goal is the object I am testing):
GotSSIInternal gotSSIInternal = (GotSSIInternal) Delegate.CreateDelegate( typeof (GotSSIInternal), target, typeof(OfflineResolver).GetMethod("GotSSI", BindingFlags.NonPublic | BindingFlags.Instance));
After that, it is very simple to call the delegate:
actual = gotSSIInternal.Invoke(out ssi, rsi);
The solution is very simple and works like a charm.