1

I need to test the CheckWarehouseAvailability private method in an abstract class.

public abstract class BaseAPI
{
    private void CheckWarehouseAvailability(Order order)
    {
        //codes here
    }
}

public class Processor : BaseAPI
{

}

Here is my TestMethod

Processor pro = new Processor();
PrivateObject privBase = new PrivateObject(pro, new PrivateType(typeof(BaseAPI)));
var retVal = privBase.Invoke("CheckWarehouseAvailability(order)");
Assert.AreEqual(true, retVal);

When I run the test method, I got this error: An exception of type 'System.MissingMethodException' occured in mscorlib.dll but was not handled in user code

Jen143
  • 815
  • 4
  • 17
  • 42
  • 2
    The argument (`order`) needs to be constructed before the call and passed as an additional argument to `Invoke`. Like it is currently written, you attempt to invoke a method which has the string "(order)" as a part of its name. – Cee McSharpface Aug 08 '17 at 08:18

2 Answers2

3

You need to instantiate the Order argument of CheckWarehouseAvailability and pass it as an argument to PrivateObject.Invoke():

var order = new Order(); 
var retVal = privBase.Invoke("CheckWarehouseAvailability", order);
// ... Assert

MSDN: PrivateObject.Invoke(String, Object[])

Also, I'm not sure what you are asserting here:

Assert.AreEqual(true, retVal);

...as CheckWarehouseAvailability has it's return type set to void, not bool.

trashr0x
  • 6,457
  • 2
  • 29
  • 39
1

First you need to get the method via reflection

MethodInfo methodName = privBase.GetType().GetMethod("ShowGrid",
           BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

then invoke the method with your params

var retVal=methodName .Invoke(privBase, order);
npo
  • 1,060
  • 8
  • 9