patterncsharpCritical
C# "internal" access modifier when doing unit testing
Viewed 0 times
modifieraccessdoingtestingunitwheninternal
Problem
I'm trying to figure out if I should start using more of
I know that if we use
This makes me think that I should just always use
Why shouldn't one do this? When should one use
internal access modifier.I know that if we use
internal and set the assembly variable InternalsVisibleTo, we can test functions that we don't want to declare public from the testing project.This makes me think that I should just always use
internal because at least each project (should?) have its own testing project.Why shouldn't one do this? When should one use
private?Solution
Internal classes need to be tested and there is an assembly attribute:
Add this to the project info file, e.g.
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]Add this to the project info file, e.g.
Properties\AssemblyInfo.cs, for the project under test. In this case "MyTests" is the test project.Code Snippets
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]Context
Stack Overflow Q#358196, score: 1563
Revisions (0)
No revisions yet.