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 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); } |
Unit Test private method in C# .NET
Subscribe to:
Post Comments (Atom)
Ok, I've done pretty much EXACTLY what you've laid out here and it flat-out refuses to work.
ReplyDeleteOn 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?
I'm replying to my earlier comment here.
DeleteMy 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.
Another related topic could be the unit test for "internal" classes from another assembly.
ReplyDeleteThat 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)