How do you unit test different access levels of a class?

I admit that I am a complete beginner when it comes to unit testing. I can easily understand the concepts (check one thing, break-fix-test-repeat, etc.), but I had a little problem with this ...

I was instructed to rewrite most of our application, and my cool structure is pretty good. We have our test projects mixed with the rest of the solution, and all links are built in the way we want. Unfortunately, there are several Friends classes that can only be accessed from the same namespace. Be that as it may, the test class is not a member of this namespace, so I cannot directly access any of these base methods that need to be tested REALLY .

From what I read, I could create a public layout of the classes in question and test it this way, but I am worried that someone will make changes to the production code along the way and not copy it to the test code, completely surpassing the purpose of testing. Another option would be to change the access level on the classes themselves, but this will require a lot of overhead and mess with the existing code. The idea to write an interface also came up, but the creation of a solid interface structure for testing did not come under control.

Am I just missing something? What would be the best way to ensure that these base classes actually function correctly without changing access to them?

+4
source share
3 answers

You can also subclass the test object, which is in the same namespace as the test object, and the subclass can reveal all the functions needed for testing.

Assuming you have some way to give this subclass a β€œtest”, you are free at home. (You do not want this class to be used in your regular code, as it breaks encapsulation)

+3
source

I'm not sure if you are referencing .NET / C # projects, but you can add the InternalsVisibleTo attribute to the AssemblyInfo.cs file to expose your internal classes in the unit test assembly.

Suppose you created a unit test project called "MyApplication.Tests", add it to the AssemblyInfo.cs file "MyApplication" (located in the "Properties" section):

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

+4
source

I think your unit tests should not require anything from the source code, so the first answer certainly works. Do you find using Reflection? I think this is about changing the source code; there is a good discussion of this here: CodeProject

0
source

All Articles