chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I thought it would be a good idea for us to be able to test private method directly.
asked by nsoonhui (59.1k points)

5 Answers

0 votes
Can you please give an example?
Not sure I understand the meaning of "test private method directly" :shock:
answered by ohad (35.4k points)
0 votes
Sorry for not making myself clear! What I meant was, I wanted to call private method directly just for unit testing purpose.

Now I understand that it is possible to call private method using reflection, but the procedure is quiet cumbersome, not sure whether Typemock can help to simplify this or not 8)
free drupal themes
answered by nsoonhui (59.1k points)
0 votes
Hi Soon,

For the purpose of testing you have a few alternatives which you may or may not like:
1. call the private method through reflection - a bit cumbersome but once you get the syntax down it gets easy
2. make the method internal, and make it accessible to the test class by using InternalsVisibleTo in Assembly.cs. This of course has the impact of making this method accessible to any other class in the same assembly - something you may not want.
3. Create an internal wrapper method that calls the private method - again, you may not want one.

Basically the most "correct" way to do it is through reflection, but it's a bit unwieldy. "Correct" and hard is a deterrent for me, so I usually go with one of the other ways.

answered by doron (17.2k points)
0 votes
You can also use:
:arrow: mstest private accessors
:arrow: mbUnit Reflector.RunNonPublicMethod
answered by eli (5.7k points)
0 votes
Hi doron, thanks for your tips.

Hi eli, unfortunately for this particular case I am using NUnit, so it may not be very helpful here..
maxim XJ750
answered by nsoonhui (59.1k points)