C# "Internal" Access Modifier When Doing Unit Testing

C# internal access modifier when doing unit testing

Internal classes need to be tested and there is an assembly attribute:

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.

C# Internal Access Modifier

I don't think so. In C#, partial classes cannot span assemblies, so I can't see a case in which another type definition in the same assembly could have different access to s than a type definition in a foreign assembly. Internal is an accessibility modifier applied only at the assembly level so, in this instance, I think internal is functionally equivalent to public. I'd be delighted to be shown to be wrong, however.

Making code internal but available for unit testing from other projects

If you're using .NET, the InternalsVisibleTo assembly attribute allows you to create "friend" assemblies. These are specific strongly named assemblies that are allowed to access internal classes and members of the other assembly.

Note, this should be used with discretion as it tightly couples the involved assemblies. A common use for InternalsVisibleTo is for unit testing projects. It's probably not a good choice for use in your actual application assemblies, for the reason stated above.

Example:

[assembly: InternalsVisibleTo("NameAssemblyYouWantToPermitAccess")]
namespace NameOfYourNameSpace
{

Unit Testing and Access modifiers in OO

Unit testing is "Black box" testing. You should only test the externally visible elements. If you test all the internal workings then you can't refactor your code correctly without modifying all your unit tests.

Proper use of internal class when testing with generic base test class

The problem you're running into is that you are trying to create a class that is more accessible than its base class.

You can delegate instead of deriving:

public class ReportServiceTests
{
private Tests tests = new Tests();

[Fact]
public async Task CreateReport_WhenCalled_LogsTheCall()
{
tests.CreateReport_WhenCalled_LogsTheCall();
}

private class Tests : TestsFor<ReportService>
{
public async Task CreateReport_WhenCalled_LogsTheCall()
{
// Act
await Instance.CreateReport();

// Assert
GetMockFor<ILogger>().Verify(logger => logger.Enter(Instance, nameof(Instance.CreateReport)), Times.Once());
}
}
}

.Net assemblies and internal access modifier usage

internal means you can 'see' it (reference, derive, etc) from classes within the same assembly (.DLL, .EXE)

Question: If in A1ClassOne I have method with access modifier
"internal", then from which classes it will be accessible?

From all classes that are within the same assembly as where that method is.

What is meant by assembly in above solution example? Whole solution? Or each
project is different assembly (two assemblies in solution)? Or each
class (.cs) file? What if there are several classes in one .cs file?

A .DLL is an assembly. And so is an .EXE Which usually corresponds to a project, yes.

ProjectA (NameSpaceA)
- ref ProjectB
- ClassA1.cs
- A1ClassOne
- internal Method1
- A1ClassTwo
- ClassA2.cs
ProjectB (NameSpaceB)
- ClassB1.cs
- ClassB2.cs

Assembly 'A' would be A1ClassOne, A1ClassTwo. Method1 can be accessed from them.

Assembly 'B' would be all classes defined in ClassB1 and ClassB2 files. They cannot access Method1.

c# access modifier section like c++

Yes, you specify it on each member. Two good things about this:

  • Moving the methods around within the same class can't affect anything.
  • You can immediately see the access modifier by looking at the method declaration. No need to look higher up in the file to see if you're in a public section or not.

Is it possible for methods to have dual access modifiers?

Why not add InternalsVisibleTo in your assembly to allow the tests access?

[assembly:InternalsVisibleTo("YourTestAssembly")]


Related Topics



Leave a reply



Submit