I trying to refactor C# code and wanted to use Microsoft Unit Testing to ensure that i don't break any functionality. The Microsoft Unit Testing recommends using Public methods with no parameters while the code has private methods with lots of parameters. How to unit test these private methods which has input parameters too using Microsoft Unit Testing Framework?
Asked
Active
Viewed 1,117 times
0
-
I'm not sure if this will work but you could just declare as a partial class, and then have a separate partial that wraps your private members as public so you can use it in testing. – johnny 5 Jun 24 '15 at 18:37
1 Answers
0
Testing methods you don't have access to will more than likely require you to use reflection. It can be a brittle pattern, but see the following for an example of getting a private method and invoking it with parameters: Reflection: How to Invoke Method with parameters
May look something like this:
MethodInfo method = myObj.GetType().GetMethod(methodName);
object result = method.Invoke(myObj, new object[] { param1, param2 });
return result;
I'm personally not a fan of using reflection to test private methods; I prefer targeting the larger, public methods which make use of these private methods.