Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I allow assembly (unit testing one) to access internal properties of another assembly?

I would like my Core assembly to not expose a certain class and I would still like to be able to test it. How can I do that ?

like image 859
Tomas Pajonk Avatar asked Sep 09 '08 13:09

Tomas Pajonk


People also ask

Should internal methods be unit tested?

Writing unit tests for internal classes ensures that they behave correctly in their own world at least and we can have integration tests to test the correctness of whole application. Even if there are some redundant unit tests, it is better than having missing test cases at the top level.

Which one of the following is a type of member that can be accessed by any code in the same assembly but not from another assembly?

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

What is a friend assembly?

A friend assembly is an assembly that can access another assembly's internal (C#) or Friend (Visual Basic) types and members. If you identify an assembly as a friend assembly, you no longer have to mark types and members as public in order for them to be accessed by other assemblies.

Which of the following attributes is required to identify one or more friend assemblies for a given assembly?

You can mark a friend assembly using the [assembly: InternalsVisibleTo(...)] attribute.


1 Answers

InternalsVisibleTo attribute to the rescue!

Just add:

[assembly:InternalsVisibleToAttribute("UnitTestAssemblyName")] 

to your Core classes AssemblyInfo.cs file

See Friend Assemblies (C# Programming Guide) for best practices.

like image 66
aku Avatar answered Sep 19 '22 16:09

aku