我一直在嘲笑从ASP.NET 5中的默认项目散布在帐户中的某些对象时遇到了麻烦,因此我无法单位测试控制器。例如,我试图模拟注册操作的代码。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Email = model.Email,
IsActive = false, NewRequest = true
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var email = _settings.GetEmailLogic();// EmailLogic(_settings);
var emailuser = new VGSEmail(_settings)
{
Id = user.Id,
FirstName = model.FirstName,
LastName = model.LastName,
ToEmail = new List<string> {model.Email},
FromEmail = _settings.GetFromEmail(),
Subject = EmailMessages.RegisterSubject(),
Body = EmailMessages.RegisterMessage(model.FirstName)
};
var message = email.CreateMailMessage(emailuser);
email.Send(message);
await SignInManager.SignInAsync(user, false, false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href="" + callbackUrl + "">here</a>");
return RedirectToAction("SelectOrganization");
}
AddErrors(result);
}
我到达usermanager.createasync,它死于null。所以我尝试以下内容:
var userManager = new Mock<ApplicationUserManager>();
var identityresult = new Mock<IdentityResult>();
identityresult.Setup(s => s.Succeeded).Returns(true);
userManager.Setup(s => s.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.ReturnsAsync(Task.FromResult(identityresult);
但是第二行不会编译并一直抱怨:
'iSetup>'不包含" returnsasync"的定义和最佳的扩展方法过载'sudenceExtensions.ReturnSasync>>(isetupsequeentialResult>>>> task>)'需要一个类型的接收器'isetupsection sepential Result>>'OnlineRePoweBsite'onlinErePoweBsite'.tests
我知道它包含returnsasync的定义,因为我一直都在使用它。
例如,以下工作正常:
userManager.Setup(s => s.FindById(It.IsAny<string>())).Returns(new ApplicationUser());
userManager.Setup(s => s.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(new ApplicationUser()));
但是我在SignManage上也有类似的麻烦。例如,第二行也不会编译。
var signInManager = new Mock<ApplicationSignInManager>(userManager, authenticationManager.Object);
signInManager.Setup(s => s.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(),It.IsAny<bool>()))
.ReturnsAsync(Task.FromResult(SignInStatus.Success));
有人可以指出我在做什么错吗?如果有人对MVC中的默认帐户controller进行了完整的单位测试,那么我将在您的债务中很大,以防我以后会遇到更多问题。
调用ReturnsAsync
返回实际返回对象而不是Task
。即:
userManager
.Setup(_ => _.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.ReturnsAsync(identityresult); //<-- This here
以及另一个
signInManager
.Setup(_ => _.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(),It.IsAny<bool>()))
.ReturnsAsync(SignInStatus.Success);
.ReturnsAsync(TValue)
基本上是.Returns(Task.FromResult(TValue))
至于使控制器更具测试,这是一个广泛的话题。
也许我在这里给我的答案
将应用程序用户和其他模型移出MVC项目
可能会有所帮助,但确实需要大量的重构才能使代码更加牢固。