使用Moq模拟内部构造函数的类型 [英] Mocking a type with an internal constructor using Moq

查看:63
本文介绍了使用Moq模拟内部构造函数的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Microsoft Sync Framework模拟一个类.它只有一个内部构造函数.当我尝试以下操作时:

I'm trying to mock a class from the Microsoft Sync Framework. It only has an internal constructor. When I try the following:

var fullEnumerationContextMock = new Mock<FullEnumerationContext>();

我收到此错误:

System.NotSupportedException:父级 没有默认的构造函数. 默认构造函数必须是 明确定义.

System.NotSupportedException: Parent does not have a default constructor. The default constructor must be explicitly defined.

这是堆栈跟踪:

System.Reflection.Emit.TypeBuilder.DefineDefaultConstructorNoLock(MethodAttributes 属性) System.Reflection.Emit.TypeBuilder.DefineDefaultConstructor(MethodAttributes 属性) System.Reflection.Emit.TypeBuilder.CreateTypeNoLock() System.Reflection.Emit.TypeBuilder.CreateType() Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType() Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type [] 接口,ProxyGenerationOptions选项) Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy,Type [] AdditionalInterfacesToProxy, ProxyGenerationOptions选项) Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy,Type [] AdditionalInterfacesToProxy, ProxyGenerationOptions选项) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy,Type [] AdditionalInterfacesToProxy, ProxyGenerationOptions选项,Object [] constructorArguments, IInterceptor []拦截器) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy,Type [] AdditionalInterfacesToProxy, ProxyGenerationOptions选项,IInterceptor []拦截器) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy,Type [] AdditionalInterfacesToProxy,IInterceptor [] 拦截器) Moq.Mock 1.<InitializeInstance>b__0() Moq.PexProtector.Invoke(Action action) Moq.Mock 1.InitializeInstance()

System.Reflection.Emit.TypeBuilder.DefineDefaultConstructorNoLock(MethodAttributes attributes) System.Reflection.Emit.TypeBuilder.DefineDefaultConstructor(MethodAttributes attributes) System.Reflection.Emit.TypeBuilder.CreateTypeNoLock() System.Reflection.Emit.TypeBuilder.CreateType() Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType() Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options) Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, IInterceptor[] interceptors) Moq.Mock1.<InitializeInstance>b__0() Moq.PexProtector.Invoke(Action action) Moq.Mock1.InitializeInstance()

我该如何解决?

推荐答案

您不能模拟没有公共构造函数的类型,因为Moq将无法实例化该类型的对象.根据您要测试的内容,您有几种选择:

You cannot mock a type that does not have a public constructor because Moq will not be able to instantiate an object of that type. Depending on what you are trying to test, you have a few options:

  1. 如果有工厂对象或其他获取FullEnumerationContext实例的方法,则可以使用它(对不起,我对同步框架不熟悉)
  2. 您可以使用私有反射来实例化FullEnumerationContext,但随后您将无法在其上模拟方法.
  3. 您可以引入一个接口和/或包装对象,该对象和/或包装对象是可被测试代码可以调用的可模拟对象.运行时实现将委托给真正的FullEnumerationContext,而测试时实现将执行所需的任何操作.

这篇关于使用Moq模拟内部构造函数的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆