mongodb – 无法实例化代理…无法找到无参数的构造函数

我正在尝试使用测试MongoDB.AspNet.Identity V2提供商的Moq创建一个单元测试.这条线让我悲伤:

var appUser = new Mock<PreRegistrationMVC.Models.ApplicationUser>();
var userStore = new Mock<MongoDB.AspNet.Identity.UserStore<PreRegistrationMVC.Models.ApplicationUser>>();

看来userStore不会正确地在这里实例化是错误.

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException was unhandled by user code
  HResult=-2147024809
  Message=Can not instantiate proxy of class: MongoDB.AspNet.Identity.UserStore`1[[MVC.Models.ApplicationUser, MVC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.
  Source=Moq
  StackTrace:
       at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
       at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
       at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
       at Moq.Mock`1.<InitializeInstance>b__2()
       at Moq.PexProtector.Invoke(Action action)
       at Moq.Mock`1.InitializeInstance()
       at Moq.Mock`1.OnGetObject()
       at Moq.Mock.GetObject()
       at Moq.Mock.get_Object()
       at Moq.Mock`1.get_Object()
       at MVC_Tests.Identity.Accounts.AccountController_Test.TestSuccessfulRegister() in c:\Users\Tim\Documents\Visual Studio 2013\Projects\PreRegistrationApp\MVC_Tests\Identity\Accounts\AccountController_Test.cs:line 108
  InnerException: 

我是Moq的全新,所以我在寻找:
Moq需要什么类型的设置来实例化?有没有一些关于UserStore类,不会很好的Moq?

谢谢阅读.

最佳答案
MOQ对于嘲笑界面是有好处的,但是对于具体的课程并不好用.所以,而不是嘲笑具体的类,请求接口:

var userStore = new Mock<IUserStore<PreRegistrationMVC.Models.ApplicationUser>>();

ApplicationUser应该是POCO,所以不需要模拟它,只需创建没有最小起订量的实例,并在测试中使用.

转载注明原文:mongodb – 无法实例化代理…无法找到无参数的构造函数 - 代码日志