我正在尝试模拟userManager的CreateAsync()方法。
我正在使用ASP.Net Core 2.1。
我嘲笑了FindByNameAsync()方法。
我在其他地方读到,在Identity 2.0中,CreateAsync()方法使用ValidateAsync方法。
测试用例:
Mock<SurveyContext> mockdb = new Mock<SurveyContext>();
var mockValidator = new Mock<IUserValidator<IdentityUser>>();
mockValidator.Setup(x => x.ValidateAsync(It.IsIn<UserManager<IdentityUser>>(), It.IsAny<IdentityUser>())).Returns(Task.FromResult(IdentityResult.Success));
var mockUserStore = new Mock<IUserStore<IdentityUser>>();
var mockUserRoleStore = mockUserStore.As<IUserRoleStore<IdentityUser>>();
var mockUserManager = new Mock<UserManager<IdentityUser>>(mockUserStore.Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<IdentityUser>>().Object,
new IUserValidator<IdentityUser>[0],
new IPasswordValidator<IdentityUser>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<IdentityUser>>>().Object);
var mockRoleStore = new Mock<IRoleStore<IdentityRole>>();
var mockRoleManager = new Mock<RoleManager<IdentityRole>>(
mockRoleStore.Object, null, null, null, null);
mockdb.Setup(x => x.Add(addMember)).Returns((Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<MemberInfo>)null);
mockdb.Setup(x => x.SaveChanges()).Returns(0);
mockUserManager.Setup(x => x.CreateAsync(It.IsAny<IdentityUser>(), "password")).Returns(Task.FromResult(IdentityResult.Success));
mockUserManager.Setup(x => x.FindByNameAsync(It.IsAny<string>()))
.Returns(Task.FromResult(It.IsAny<IdentityUser>()));
mockUserManager.Setup(x => x.AddToRoleAsync(new IdentityUser()
{
UserName = "username"
}, "password")).Returns(Task.FromResult(IdentityResult.Success));
mockRoleManager.Setup(x => x.RoleExistsAsync("")).ReturnsAsync(true);
mockRoleManager.Setup(x => x.CreateAsync(new IdentityRole())).Returns(Task.FromResult(IdentityResult.Success));
每当我使用与预期相反的createasync方法时 我得到的结果为空。
我在做什么错了?