I am trying to unit test private method. I saw example below on this question
Class target = new Class();
PrivateObject obj = new PrivateObject(target);
var retVal = obj.Invoke("PrivateMethod");
Assert.AreEqual(retVal);
My private method has 2 ref params. How to pass them?
If you pass the argument array, then any ref
parameters will be populated in place:
bool p1 = true; // can be others values
bool p2 = false; // can be others values
object[] args = new object[2] { p1, p2 };
var retval = obj.Invoke("PrivateMethod", args);
p1 = (bool)args[0];
p2 = (bool)args[1];
First create an object array of your parameters. the array should then contain the new references:
Class target = new Class();
PrivateObject obj = new PrivateObject(target);
object[] args = new object[] {arg1, arg2};
var retVal = obj.Invoke("PrivateMethodWithArgs", args);
Assert.AreEqual(retVal);
Debug.WriteLine(args[0]);
Debug.WriteLine(args[1]);
Try This:
object [] myarray=new object[]{param1,param2};
var retVal = obj.Invoke("PrivateMethod",ref myarray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With