2015-05-06 39 views
4

我在Visual Studio 2013中使用最新的Moq(4.2.1502.911)來模擬包含在另一個項目中的內部接口。兩個項目都使用相同的密鑰進行簽名。搜索在這裏和其他地方在網絡上後,我算了一下,添加InternalsVisibleTo用於測試組件和「DynamicProxyGenAssembly2」,利用起訂量的公鑰像這樣:在簽名程序集的內部接口上使用Moq的TypeLoadException

[assembly: InternalsVisibleTo("UnitTestProject1, PublicKey=00240000048000009... 
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=002400000... 

現在,當我跑我的測試方法,我仍然得到以下錯誤:

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 

(英文: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.

有別的做的就是這個工作? 這裏是一個最小的編譯例子:https://www.dropbox.com/s/c2slwq4do5ttp9p/MoqTest.zip?dl=0

+0

無法從內部接口派生...以下問題http://stackoverflow.com/questions/8606412/create-type -that-implements-internal-interface可能會給你一些希望...... –

+0

不正確,這就是InternalsVisibleToAttribute的用途。當程序集沒有被簽名時,上述工作。 – Asik

+0

所以你能夠改變「另一個」項目 - 沒有意識到這一點。請注意,您的示例顯示了不同於公鑰的錯誤...通常,測試/代理程序集未簽名 - 您可能需要實際手動構建使用正確密鑰簽名的代理,才能使其正常工作。 –

回答

8

使用下面的公共密鑰值來解決這個問題對我來說:

0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7

我最初嘗試使用SN.EXE獲得公共密鑰,但我認爲,它返回的公鑰是Moq而不是DynamicProxyGenAssembly2。我從起訂量GitHub的頁面上面的鍵:

https://github.com/Moq/moq4/wiki/Quickstart

// This assembly is the default dynamic assembly generated Castle DynamicProxy, 
// used by Moq. Paste in a single line. 
[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]