I am using the latest Moq (4.2.1502.911) in Visual Studio 2013 to mock an internal interface contained in another project. Both projects are signed with the same key. After searching here and elsewhere on the web I figured I had to add InternalsVisibleTo for the test assembly and "DynamicProxyGenAssembly2", using Moq's public key like so:
[assembly: InternalsVisibleTo("UnitTestProject1, PublicKey=00240000048000009...
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=002400000...
Now when I run my test method I still get the following error:
Test Name: TestMethod1
Test FullName: UnitTestProject1.Class1Test.TestMethod1
Test Source: c:\Users\******\Documents\Visual Studio 2013\Projects\MoqTest\UnitTestProject1\UnitTest1.cs : line 13
Test Outcome: Failed
Test Duration: 0:00:00,0693592
Result Message:
Test method UnitTestProject1.Class1Test.TestMethod1 threw exception:
System.TypeLoadException: Le type 'Castle.Proxies.IFooProxy' de l'assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' essaye d'implémenter une interface inaccessible.
Result StackTrace:
à System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
à System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
à System.Reflection.Emit.TypeBuilder.CreateType()
à Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CreateType(TypeBuilder type)
à Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
à Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
à Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__DisplayClass1.<GenerateCode>b__0(String n, INamingScope s)
à Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
à Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
à Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
à Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
à Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
à Moq.Mock`1.<InitializeInstance>b__2()
à Moq.PexProtector.Invoke(Action action)
à Moq.Mock`1.InitializeInstance()
à Moq.Mock`1.OnGetObject()
à Moq.Mock`1.get_Object()
à UnitTestProject1.Class1Test.TestMethod1() dans c:\Users\******\Documents\Visual Studio 2013\Projects\MoqTest\UnitTestProject1\UnitTest1.cs:ligne 15
(In English: System.TypeLoadException : Type 'Castle.Proxies.IFooProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' is attempting to implement an inaccessible interface.
)
Is there something else to do to get this to work? Here is a minimal compilable example: https://www.dropbox.com/s/c2slwq4do5ttp9p/MoqTest.zip?dl=0