我们最近将我们的自动化单元测试从 MSTest 切换到 VStest。在切换几个单元测试开始失败后,给出以下错误消息,
测试方法 Domain.Model.IntegrationTests.Documents.DocumentFilesRepositoryTest.Delete_file_Page_not_shared_can_delete 抛出异常:
System.InvalidCastException:无法将“System.__ComObject”类型的 COM 对象转换为接口类型“Microsoft.Runtime.Hosting.IClrStrongName”。此操作失败,因为 IID 为“{9FD93CCF-3280-4391-B3A9-96E1CDE77C8D}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:未注册接口(来自 HRESULT 的异常:0x80040155)。
结果堆栈跟踪:
在 System.StubHelpers.StubHelpers.GetCOMIPFromRCW(对象 objSrc,IntPtr pCPCMD,IntPtr& ppTarget,布尔& pfNeedsRelease)在 Microsoft.Runtime.Hosting.IClrStrongName.StrongNameGetPublicKey(字符串 pwzKeyContainer,字节 [] pbKeyBlob,Int32 cbKeyBlob,IntPtr& ppbPublicPubKeyB) Microsoft.Runtime.Hosting.StrongNameHelpers.StrongNameGetPublicKey(String pwzKeyContainer, Byte[] bKeyBlob, Int32 cbKeyBlob, IntPtr& ppbPublicKeyBlob, Int32& pcbPublicKeyBlob) 在 System.Reflection.StrongNameKeyPair.ComputePublicKey() 在 System.Reflection.StrongNameKeyPair.get_PublicKey() 在 System. Reflection.Emit.AssemblyBuilder..ctor(AppDomain 域,AssemblyName 名称,AssemblyBuilderAccess 访问权限,字符串目录,证据证据,PermissionSet requiredPermissions,PermissionSet optionalPermissions,PermissionSet deniedPermissions, StackCrawlMark& stackMark, IEnumerable'1 unsafeAssemblyAttributes, SecurityContextSource securityContextSource) at System.Reflection.Emit.AssemblyBuilder.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet deniedPermissions, StackCrawlMark& stackMark, IEnumerable '1 unsafeAssemblyAttributes, SecurityContextSource securityContextSource) at System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name, >AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet >requiredPermissions, PermissionSet optionalPermissions, PermissionSet deniedPermissions, >StackCrawlMark& stackMark, IEnumerable`1 assemblyAttributes,SecurityContextSource >securityContextSource) at System.AppDomain.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess >access) at Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName) at Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName() at Castle.DynamicProxy.ModuleScope.ObtainDynamicModule(Boolean isStrongNamed) at Castle.DynamicProxy.Generators.Emitters.ClassEmitter.CreateTypeBuilder(ModuleScope modulescope, String name, Type baseType, Type[] 接口, TypeAttributes flags, Boolean forceUnsigned) at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope,>String 名称,Type baseType,Type[] 接口,TypeAttributes 标志,Boolean forceUnsigned) at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, >Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, >String name, Type baseType, Type[] interfaces) 在 Castle.DynamicProxy.Generators 中的字符串名称、类型 baseType、Type[] 接口、TypeAttributes 标志。 BaseProxyGenerator.BuildClassEmitter(String typeName, >Type parentType, Type[] interfaces) at Castle.DynamicProxy.Generators.BaseProxyGenerator.BuildClassEmitter(String typeName, >Type parentType, IList interfaceList) at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[ ] 接口,ProxyGenerationOptions 选项)在 Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) 在 Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy,Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] 拦截器) at Rhino.Mocks.MockRepository.MockClass(CreateMockState mockStateFactory,在 Rhino.Mocks.MockRepository.CreateMockObject(Type [] extras, Object[] argumentsForConstructor) 在 Rhino.Mocks.MockRepository.CreateMockObject(Type type, CreateMockState factory, Type[] extras, Object[] argumentsForConstructor) 在 Rhino.Mocks.MockRepository.PartialMultiMock(Type type, Type[] extraTypes, Object[] argumentsForConstructor) 在 Rhino.Mocks.MockRepository.PartialMock(Type type, Object[] argumentsForConstructor) 在 Rhino.Mocks.MockRepository.PartialMock[T](Object[] argumentsForConstructor) 在模型。IntegrationTests.Documents.DocumentFilesRepositoryTest.Delete_file_Page_not_shared_can_delete() 在 c:\TFS_DATA\Dev\BUILDCI\Common8\Tests\Integration\Domain.IntegrationTests\Documents\DocumentFilesRepositoryTest.cs:line 293
我们的单元测试使用 RhinoMocks 进行模拟。最初我们怀疑这可能是 RhinoMocks 的问题,因为 RhinoMocks 程序集的目标框架是 .NET 2.0,而我们使用的是 .NET 4.5。因此,我们使用 Moq 进行模拟重写了失败的测试,因为 Moq 程序集可用于目标框架 4.0。但我们仍然遇到同样的问题。任何建议错误的根本原因可能是什么?