How to do internal interfaces visible for Moq?

时间秒杀一切 提交于 2019-12-20 09:57:27

问题


I have 3 project in my C# solution.

  • Signatures
  • Structures
  • Tests

Signatures has public and internal interfaces. Also it has

  [assembly: InternalsVisibleTo("Structures")]
  [assembly: InternalsVisibleTo("Tests")]

in AssemblyInfo.cs of.

Structures has public and internal classes and

  [assembly: InternalsVisibleTo("Tests")]

in AssemblyInfo.cs of.

Tests has next source:

<packages>
  <package id="Moq" version="4.2.1409.1722" targetFramework="net45" />
  <package id="NUnit" version="2.6.4" targetFramework="net45" />
  <package id="NUnitTestAdapter" version="1.2" targetFramework="net45" />
</packages>

as NuGet packages in packages.config.

I wrote some unit test for internal interface from Signatures and internal class from Structures. Run, and had next result: exception:

Type Signatures.InterfaceX is not visible to DynamicProxy. Can not create proxy for types that are not accessible. Make the type public, or internal and mark your assembly with [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] attribute.

Seems logical. I added [assembly: InternalsVisibleTo("InternalsVisible.DynamicProxyGenAssembly2")] to assembly info of Signatures and Structures projects. Run, and had next result: exception:

Type 'Castle.Proxies.IReminiscenceableDataTableProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.

This was to help, but didn't. Only change exception message.

How to fix my problem?


回答1:


The suggested fix message uses a const/static field for the assembly name:

[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

You used a string which does not correspond to the assembly name:

[assembly: InternalsVisibleTo("InternalsVisible.DynamicProxyGenAssembly2")] 

Change it to:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] 

You can see the actual assembly name (which should appear in InternalsVisibleTo) in your error message:

Type 'Castle.Proxies.IReminiscenceableDataTableProxy' from assembly 'DynamicProxyGenAssembly2 (...)



来源:https://stackoverflow.com/questions/28234369/how-to-do-internal-interfaces-visible-for-moq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!