Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to PrivateObject method [duplicate]

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?

like image 896
eomeroff Avatar asked Mar 18 '14 19:03

eomeroff


3 Answers

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];
like image 56
Lee Avatar answered Nov 18 '22 02:11

Lee


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]);
like image 25
D Stanley Avatar answered Nov 18 '22 02:11

D Stanley


Try This:

object [] myarray=new object[]{param1,param2};
var retVal = obj.Invoke("PrivateMethod",ref myarray);
like image 1
Sudhakar Tillapudi Avatar answered Nov 18 '22 02:11

Sudhakar Tillapudi