I have a C# project library DLL with an internal class, and I want to unit test it. I want to specifically unit test this internal class by getting a reference to it using reflection, if possible.
I have this class:
namespace ProjectA.B.C
{
class Caching
{
public static void DoWork() { }
}
}
How can I call the DoWork
method using reflection from another project that uses ProjectA
? I have tried, but my first issue is that typeof(ProjectA.B.C.Caching)
is protected so I can't seem to even get the type reference yet, but I'm sure there is a way.
The error is :
'ProjectA.B.C.Caching' is inaccessible due to its protection level
You can use the InternalsVisibleToAttribute to make the internal class visible to your unit test project.
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx
If the other project has the name
ProjectA.B.C
and spits out an assembly namedProjectA.B.C
, and it has the following internal class implementation with the following method signatures and the following namespace:You can call it like this for unit testing:
The above shows how you can:
MakeGenericMethod(params Type[] typeArguments)
.out
parameters and check the returned parameters array for the result.