Before we start to see, how a private method can be unit tested using the MS Test? Let us discuss whether it is a good idea to test a private method or not? Often I have seen there are two school of thoughts,
- Private methods should be tested.
- Private methods should not be tested.
To put these things in perspective, let us consider a system under test (SUT) Salary class as shown in the listing below.
A system under test Salary class has two functions:
- The CalculateSal method is a public method and it takes two parameters to calculate the salary
- The isValidwd method is a private method and it takes one parameter. This function returns true if the number of working days are more than 8, or else it returns false.
- The CalculateSal method first checks whether the number of working days is valid or not using the private method isValidWd.
- If the number of working days is valid, then salary is calculated by the multiplication of basic salary and the number of working days, or else it is fixed at $1000.