C# 为什么Mock.SetupAllProperties引发构造函数异常?

C# 为什么Mock.SetupAllProperties引发构造函数异常?,c#,asp.net,unit-testing,moq,C#,Asp.net,Unit Testing,Moq,我使用Moq来驱动我的模型视图演示器单元测试。我正在测试的特定视图由一个子视图组成,该子视图被建模为一个接口,如下所示: public interface ICustomerView:IView{ public int CustomerId {get;set;} public IDateRangeView DateRangeView {get;set;} } 在某些测试中,我不想测试IDateRangeView的行为或交互;我只是希望对该属性的任何调用都不要抛出空引用异常等

我使用Moq来驱动我的模型视图演示器单元测试。我正在测试的特定视图由一个子视图组成,该子视图被建模为一个接口,如下所示:

public interface ICustomerView:IView{

    public int CustomerId {get;set;}
    public IDateRangeView DateRangeView {get;set;}

}
在某些测试中,我不想测试IDateRangeView的行为或交互;我只是希望对该属性的任何调用都不要抛出空引用异常等。Moq似乎通过将模拟的DefaultValue设置为DefaultValue.mock,然后调用SetupAllProperties来支持此功能:

[Test]
public void some_test(){

     var mockView = new Mock<ICustomerView>() { DefaultValue = DefaultValue.Mock };
     mockView.SetupAllProperties();
}
[测试]
公共无效测试(){
var mockView=new Mock(){DefaultValue=DefaultValue.Mock};
mockView.SetupAllProperties();
}
但是,调用SetupAllProperties会引发以下异常:

System.ArgumentException occurred
  Message="A matching constructor for the given arguments was not found on the mocked type."
  Source="Moq"
  StackTrace:
       at Moq.Mock`1.<InitializeInstance>b__0()
       at Moq.PexProtector.Invoke(Action action)
       at Moq.Mock`1.InitializeInstance()
       at Moq.Mock`1.get_Object()
       at Moq.Mock`1.GetObject()
       at Moq.Mock.get_Object()
       at Moq.MockDefaultValueProvider.ProvideDefault(MethodInfo member, Object[] arguments)
       at Moq.Mock.<>c__DisplayClass27.<SetupAllProperties>b__23()
       at Moq.PexProtector.Invoke(Action action)
       at Moq.Mock.SetupAllProperties(Mock mock)
       at Moq.Mock.<>c__DisplayClass27.<SetupAllProperties>b__23()
       at Moq.PexProtector.Invoke(Action action)
       at Moq.Mock.SetupAllProperties(Mock mock)
       at Moq.Mock`1.SetupAllProperties()

  InnerException: System.MissingMethodException
       Message="Constructor on type 'DateRangeProxyf68a22c875334e7d858d8ccc5759ecaa' not found."
       Source="mscorlib"
       StackTrace:
            at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
            at System.Activator.CreateInstance(Type type, Object[] args)
            at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
            at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
            at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, IInterceptor[] interceptors)
            at Moq.Mock`1.<InitializeInstance>b__0()
       InnerException: 
发生System.ArgumentException

Message=“在模拟类型上找不到与给定参数匹配的构造函数。”
来源=“最小起订量”
堆栈跟踪:
最低起订量模拟'1.b_uu0()
在Moq.PexProtector.Invoke(操作)
在Moq.Mock`1.InitializeInstance()
在Moq.Mock`1.get_Object()
在Moq.Mock`1.GetObject()处
在Moq.Mock.get_Object()处
在Moq.MockDefaultValueProvider.ProviderDefault(MethodInfo成员,对象[]参数)处
在Moq.Mock.c_uuu显示类27.b_uuuu23()
在Moq.PexProtector.Invoke(操作)
最小起订量时。模拟。设置所有属性(模拟)
在Moq.Mock.c_uuu显示类27.b_uuuu23()
在Moq.PexProtector.Invoke(操作)
最小起订量时。模拟。设置所有属性(模拟)
在Moq.Mock`1.SetupAllProperties()处
InnerException:System.MissingMethodException
Message=“找不到类型为“DateRangeProxyf68a22c875334e7d858d8ccc5759ecaa”的构造函数。”
Source=“mscorlib”
堆栈跟踪:
位于System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr、Binder Binder、Object[]args、CultureInfo区域性、Object[]ActivationAttribute)
位于System.Activator.CreateInstance(类型,对象[]args)
位于Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(类型classToProxy,类型[]additionalInterfacesToProxy,类型[]additionalInterfacesToProxy,ProxyGenerationOptions,对象[]构造函数参数,IInterceptor[]拦截器)
在Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(类型classToProxy,类型[]additionalInterfacesToProxy,ProxyGenerationOptions,IIInterceptor[]拦截器)
在Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(类型classToProxy,类型[]additionalInterfacesToProxy,IIInterceptor[]拦截器)
最低起订量模拟'1.b_uu0()
内部异常:
我的工作是手动设置此类接口,但必须有一个不那么繁琐的解决方案:

 [Test]
    public void some_test(){

         var mockView = new Mock<ICustomerView>();
         var viewDateMock = new Mock<IDateRangeView>();
         mockView.SetupGet(v=>v.DateRangeView).Returns(viewDateMock.Object);
    }
[测试]
公共无效测试(){
var mockView=new Mock();
var viewDateMock=new Mock();
SetupGet(v=>v.DateRangeView).Returns(viewDateMock.Object);
}
版本/环境

最低起订量3.1.416.3
.NET Framework 3.5
nUnit 2.5.0.9122

VS 2008+Windows 7 x64

抱歉,您可以将此归咎于用户错误。抛出的异常是准确的。IDateRangeView(DateRange)上的一个属性确实缺少无参数构造函数。我想我期望SetupAllProperties只在所讨论的模拟的直接子对象上创建自动模拟,而不尝试模拟整个对象图。