tags:

views:

334

answers:

3
+1  Q: 

Nunit Testing

How do I test, private methods and internal classes using Nunit.

A: 

You have to expose a means to invoke them, possibly through a test-specific derived class.

McWafflestix
+3  A: 

I typically don't. If you thoroughly test the public methods that use private methods and internal classes then you should be able to test the full range of the private functionality without exposing it.

Bill the Lizard
+6  A: 

Private methods:

If you're trying to test non-public methods, it usually means you're doing it wrong.

If there's functionality that you want to test, but don't want to make public on your class, the code is trying to tell you something. Your class probably has too many responsibilities. You should seriously consider extracting that private functionality into a new class, writing tests for the new class, and making your old class have a private instance of the new class.

Internal classes:

This one is more valid, especially if you're writing a class library for others to reuse. You may have classes that aren't designed for general use, but that you want to write unit tests for.

For this case, take a look at InternalsVisibleToAttribute.

Joe White