Last Updated: February 25, 2016
·
842
· etgryphon

Use of 'internal' in C# for Unit Testing

I'm learning C# for my new job and there seem to be an over-abundance of reserved words to use. One I came across is internal vs private. Where internal is similar to private, but the scope is in the assembly. I found this to be a bit silly, but then I came across a use that makes perfect sense to me. Creating APIs to access private variables for unit testing. To me, this is a great idea because there are often times that I would like to test internal items in isolation, but do not want to make APIs for others to use.

1 Response
Add your response

Also take a look at [InternalsVisibleTo], which lets you use the same technique you've discovered but also extend it to certain select assemblies (useful for when you don't want your unit tests in the same assembly as your "real" code):

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

over 1 year ago ·