Making code internal but available for unit testing from other projects

前端 未结 4 1669
走了就别回头了
走了就别回头了 2020-12-04 10:26

We put all of our unit tests in their own projects. We find that we have to make certain classes public instead of internal just for the unit tests. Is there anyway to avo

4条回答
  •  余生分开走
    2020-12-04 11:20

    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
    {
    

提交回复
热议问题