mongodb 无法实例化代理...找不到无参数构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25649155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not instantiate proxy...Could not find a parameterless constructor
提问by ChiliYago
I am trying to create a unit test using Moq which tests the MongoDB.AspNet.Identity V2provider. This line is giving me grief:
我正在尝试使用测试MongoDB.AspNet.Identity V2提供程序的Moq 创建单元测试。这条线让我感到悲伤:
var appUser = new Mock<PreRegistrationMVC.Models.ApplicationUser>();
var userStore = new Mock<MongoDB.AspNet.Identity.UserStore<PreRegistrationMVC.Models.ApplicationUser>>();
It seems the userStore won't instantiate properly here is the error.
似乎 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:
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 需要什么类型的设置来实例化它?UserStore 类是否有一些不适用于 Moq?
Thanks for reading.
谢谢阅读。
回答by trailmax
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:
MOQ 适用于模拟接口,但不适用于具体类。所以不要嘲笑具体的类,而是要求接口:
var userStore = new Mock<IUserStore<PreRegistrationMVC.Models.ApplicationUser>>();
Also ApplicationUser
should be POCO, so no need to mock it, just create its instance without MOQ and use in tests.
也ApplicationUser
应该是 POCO,所以不需要模拟它,只需在没有 MOQ 的情况下创建它的实例并在测试中使用。
回答by Remotec
I had this problem. I had written...
我有这个问题。我写过...
var x = new Mock<Concrete>();
... instead of ...
... 代替 ...
var x = new Mock<IConcrete>();
回答by Ashraf Alam
You can try referring Mock behavior,as shown below
可以尝试参考Mock行为,如下图
Mock<testClass>(MockBehavior.Strict, new object[] {"Hello"});