Unit Test private method in C# .NET


This is a very common c# interview question. As a developer all of us know, how to unit test public members of a class. All you do is create an instance of the respective
class and invoke the methods using the created instance. So, unit testing public methods is very straight forward, but if the method that we want to unit test is a private method, then we cannot access it outside the class and hence cannot easily unit test it.


Consider the example class shown below. CalculatePower() method with in the Maths class is private and we want to unit test this method. Also, note that CalculatePower() is an instance private method. In another articel we will discuss the concept of unit testing a private static method. Microsoft's unit testing assembly contains a class called PrivateObject, which can be used to unit test private methods very easily. Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject is the fully qualified name. Click here, to read an articel on, unit testing a private static method with an example.

public class Maths
{
   private int CalculatePower(int Base, int Exponent)
   {
      int Product = 1;

      for (int i = 1; i <= Exponent; i++)
      {
         Product = Product * Base;
      }
      return Product;
   }
}

To unit test this method, We create an instance of the class and pass the created instance to the constructor of PrivateObject class. Then we use the instance of the PrivateObject class, to invoke the private method. The fully completed unit test is shown below.

[TestMethod()]
public void CalculatePowerTest()
{
   Maths mathsclassObject = new Maths();
   PrivateObject privateObject = new PrivateObject(mathsclassObject); 
   object obj = privateObject.Invoke("CalculatePower", 2, 3); 

   Assert.AreEqual(8, (int)obj);
}

3 comments:

  1. Ok, I've done pretty much EXACTLY what you've laid out here and it flat-out refuses to work.

    On the ".Invoke" line I receive the following error message, "An exception of type 'System.MissingMethodException' occurred in mscorlib.dll but was not handled in user code. Additional information: Attempted to access a missing member."

    What now?

    ReplyDelete
    Replies
    1. I'm replying to my earlier comment here.

      My bad. I figured out my own problem.

      I let myself be mislead by the statement, "Attempted to access a MISSING member." That message would have been more accurate if it read something like, "There is no signature for the method 'CalculatePower' that accepts the provided parameters." My method was looking for a DateTime parameter but I was trying to pass it an int. Once I changed the input value to a DateTime it worked fine, obviously.

      Delete
  2. Another related topic could be the unit test for "internal" classes from another assembly.
    That is possible keeping the "internal" accessor for the classes to test and using the attribute "[assembly: InternalsVisibleTo("x")]" into the unit test class pointing towards the assembly that we need (the one that contains the "internal" classes)

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us