【发布时间】:2015-01-21 10:42:17
【问题描述】:
我在使用 nsubstitute 和 nunit 模拟 ApplicationUserManager 类以测试我的操作方法时遇到问题。这是我嘲笑班级的方式。
var _userManager = Substitute.For<ApplicationUserManager>();
在我的测试系统中,我正在使用构造函数注入来注入类。当我运行测试时,我收到此错误消息。
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: JobHub.Web.Identity.ApplicationUserManager.
Could not find a parameterless constructor.
我的问题是如何像使用类的 SetPhoneNumberAsync()method 一样使用 NSubstitue 正确模拟这个类。
编辑 顺便说一句,这是我要测试的一段代码
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(UserProfileView model)
{
if (ModelState.IsValid)
{
var userId = User.Identity.GetUserId();
var profile = model.MapToProfile(userId);
if (CommonHelper.IsNumerics(model.PhoneNo))
{
await _userManager.SetPhoneNumberAsync(userId, model.PhoneNo);
}
if (model.ProfileImage != null)
{
profile.ProfileImageUrl = await _imageService.SaveImage(model.ProfileImage);
}
_profileService.AddProfile(profile);
_unitofWork.SaveChanges();
//Redirect to the next page (i.e: setup experiences)
return RedirectToAction("Skills", "Setup");
}
return View("UserProfile", model);
}
【问题讨论】:
标签: asp.net-mvc-5 nunit nsubstitute