How to test internal classes

When you're writing unit tests you may sometimes find yourself in a situation where you need to be able to access an internal class from within another project such as your *.Tests project.

Your tests should always be placed in a separate project and not be mixed in with your implementation code but since internal classes by default are not accessible from other projects (which is kind of the whole point) you might think these classes are not testable and that they need to be made public in order for them to be tested.

But luckily there is a way around this. You can configure it so that your *.Tests project can always access any internal class in your implementation projects and then you never have to think of this problem ever again.

In your implementation project, add this snippet in your AssemblyInfo.cs file located in your Properties folder:

[assembly: InternalsVisibleTo("SolutionName.Tests")]

Replace SolutionName with the actual name of your Solution.

Now your *.Tests project will be able to access any internal class in your implementation project.

Sometimes it's that easy..

Cheers!