无法实例化代理...找不到无参数的构造函数 [英] Can not instantiate proxy...Could not find a parameterless constructor

查看:94
本文介绍了无法实例化代理...找不到无参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Moq创建一个单元测试,该单元测试用于测试 MongoDB.AspNet .Identity V2 提供程序.这行让我很伤心:

I am trying to create a unit test using Moq which tests the MongoDB.AspNet.Identity V2 provider. This line is giving me grief:

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

看来userStore无法正确实例化,这是错误所在.

It seems the userStore won't instantiate properly here is the error.

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不能很好地发挥作用吗?

I am completely new to Moq so I am looking for: What type of settings are needed for Moq to instantiate this? Is there something about the UserStore class that won't play well with Moq?

感谢阅读.

推荐答案

MOQ对于模拟接口很有用,但在具体类上不能很好地工作.因此,不要嘲笑具体的类,而是要求提供接口:

MOQ is good for mocking interfaces, but does not work so well with concrete classes. So instead of mocking concrete class, ask for the inteface:

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

ApplicationUser也应该是POCO,因此无需模拟它,只需创建没有MOQ的实例并在测试中使用即可.

Also ApplicationUser should be POCO, so no need to mock it, just create its instance without MOQ and use in tests.

这篇关于无法实例化代理...找不到无参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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