I recently have to unit test a couple classes implemented by another developer. He implements these classes as internal which causes some problems when trying to unit test them. Internal classes cannot be referenced in another project (or assemblies).
First thing comes to mind is to make these classes public so they can be referenced in the unit test project. However, if there is a valid reason for keeping them internal, there is a way these classes can be exposed to only the unit test project/assembly.
InternalsVisibleTo
InternalsVisibleTo attribute allows you to specify which assembly you want to make the internal/sealed class visible to. Simply place it at the top of your class where all the using statements are.
using System; [assembly: InternalsVisibleTo("UnitTest")] internal class SealedClassExample { }
UnitTest is the name of the specified assembly that you want the internal/seal class to be visible to.
If you want to make the entire assembly visible, so you don’t have to specify each file individually, you could add this attribute in the AssemblyInfo.cs file.
Tom Phan
That’s what I said. Putting it in AssemblyInfo.cs will make all internal classes and methods to the destination assembly.
JSCC
“If you want to make the entire assembly visible, so you don’t have to specify each file individually, you could add this attribute in the AssemblyInfo.cs file.” — that’s not true AFAIK. The little “assembly: ” means that the attribute is assembly-wide no matter what file one uses it in.
AssemblyInfo.cs is just (by convention) a place one would look assembly-affecting attributes since they usually don’t have a meaningful reason being anywhere (else).
business startup loan
Hi there, just wanted to tell you, I loved this blog post. It was practical. Keep on posting!
education online courses
always i used to read smaller posts that as well clear their motive, and that is also happening with this piece of writing which I am reading at this time.