public async Task Index_ReturnsViewBagMessagesExpected() { // Arrange var userId = "TestUserA"; var phone = "abcdefg"; var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userId) }; var userManager = _serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); var userManagerResult = await userManager.CreateAsync( new ApplicationUser { Id = userId, UserName = "******", TwoFactorEnabled = true, PhoneNumber = phone }, "Pass@word1"); Assert.True(userManagerResult.Succeeded); var signInManager = _serviceProvider.GetRequiredService <SignInManager <ApplicationUser> >(); var httpContext = _serviceProvider.GetRequiredService <IHttpContextAccessor>().HttpContext; httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims)); var controller = new ManageController() { UserManager = userManager, SignInManager = signInManager, }; controller.ActionContext.HttpContext = httpContext; // Act var result = await controller.Index(); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.Empty(controller.ViewBag.StatusMessage); Assert.NotNull(viewResult.ViewData); var model = Assert.IsType <IndexViewModel>(viewResult.ViewData.Model); Assert.True(model.TwoFactor); Assert.Equal(phone, model.PhoneNumber); Assert.True(model.HasPassword); }
public async Task Index_ReturnsViewBagMessagesExpected() { // Arrange var userId = "TestUserA"; var phone = "abcdefg"; var claims = new List<Claim> { new Claim(ClaimTypes.NameIdentifier, userId) }; var userManager = _serviceProvider.GetRequiredService<UserManager<ApplicationUser>>(); var userManagerResult = await userManager.CreateAsync( new ApplicationUser { Id = userId, UserName = "******", TwoFactorEnabled = true, PhoneNumber = phone }, "Pass@word1"); Assert.True(userManagerResult.Succeeded); var signInManager = _serviceProvider.GetRequiredService<SignInManager<ApplicationUser>>(); var httpContext = _serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext; httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims)); var controller = new ManageController() { UserManager = userManager, SignInManager = signInManager, }; controller.ActionContext.HttpContext = httpContext; // Act var result = await controller.Index(); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.Empty(controller.ViewBag.StatusMessage); Assert.NotNull(viewResult.ViewData); var model = Assert.IsType<IndexViewModel>(viewResult.ViewData.Model); Assert.True(model.TwoFactor); Assert.Equal(phone, model.PhoneNumber); Assert.True(model.HasPassword); }