vendredi 3 septembre 2021

How to unit test an internal method or private method in an abstract class in c#?

I have to write a unit test for an internal or private method in an abstract class in c#. consider this:

Public Abstract Class MyAbstractClass
{
 Private String MyMethod()
 {
   //Do Something
   return "HI";
 }
}

And, my test method:

public void MyTestMethod()
{
    var testProcessor = new Mock<MyAbstractClass>
    {
        CallBase = true
    };
    var privateType = new PrivateType(typeof(MyAbstractClass));
    PrivateObject privateObject = new PrivateObject(testProcessor , privateType);
    var resValue = (String)privateObject.Invoke("MyMethod");
    Assert.AreEqual(resValue ,"HI");
}

but when I run the test method, I'm getting below error:

 System.Reflection.TargetException: Object does not match target type.

How to resolve that?





Aucun commentaire:

Enregistrer un commentaire