public async Task AddUserPost_ShouldReturnRedirectToActionWhenModelStateIsValid() { // Arrange string tempDataSuccessMessage = null; RegisterViewModel formModel = new RegisterViewModel() { Email = FirstUserEmail, Username = FirstUserUsername, }; Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); userManager .Setup(x => x.CreateAsync(It.IsAny <User>(), It.IsAny <string>())) .ReturnsAsync(IdentityResult.Success); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData.SetupSet(t => t["SuccessMessage"] = It.IsAny <string>()) .Callback((string key, object successMessage) => tempDataSuccessMessage = successMessage as string); HomeController controller = new HomeController(null, userManager.Object, null); controller.TempData = tempData.Object; // Act IActionResult actionResult = await controller.AddUser(formModel); // Assert actionResult.Should().BeOfType <RedirectToActionResult>(); actionResult.As <RedirectToActionResult>().ActionName.Should().Be("AllUsers"); tempDataSuccessMessage.Should().Be(AddUserMessageSuccessfull); }
public async Task AddUserPost_ShouldReturnViewWithModelWhenCreateAsyncFailed() { // Arrange RegisterViewModel formModel = new RegisterViewModel() { Username = "******" }; Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); userManager .Setup(x => x.CreateAsync(It.IsAny <User>(), It.IsAny <string>())) .ReturnsAsync(IdentityResult.Failed(new IdentityError { Description = "test" })); HomeController controller = new HomeController(null, userManager.Object, null); // Act IActionResult actionResult = await controller.AddUser(formModel); // Assert actionResult.Should().BeOfType <ViewResult>(); object model = actionResult.As <ViewResult>().Model; model.Should().BeOfType <RegisterViewModel>(); var returnedModel = model.As <RegisterViewModel>(); returnedModel.Username.Should().Be(formModel.Username); }
public async Task ManageRolesPost_ShouldReturnNotFoundWhenInvalidUserId() { // Arrange Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); HomeController controller = new HomeController(null, userManager.Object, null); // Act IActionResult actionResult = await controller.ManageRoles(InvalidUserId, new IdentityRoleViewModel()); // Assert actionResult.Should().BeOfType <NotFoundResult>(); }
public async Task ChangeUserRolesPost_ShouldReturnNotFoundWhenInvalidUserId() { // Arrange Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); UsersController controller = new UsersController(null, null, userManager.Object); // Act IActionResult actionResult = await controller.ChangeUserRoles(InvalidUserId, new ChangeUserRolesFormModel()); // Assert actionResult.Should().BeOfType <NotFoundResult>(); }
public async Task DeleteUserPost_ShouldReturnNotFoundWhenInvalidUserId() { // Arrange Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); HomeController controller = new HomeController(null, userManager.Object, null); // Act IActionResult actionResult = await controller.DeleteUser(InvalidUserId); // Assert actionResult.Should().BeOfType <NotFoundResult>(); }
private Mock <UserManager <User> > GetAndSetUserManagerMock() { Mock <UserManager <User> > userManager = UserManagerMock.GetNew(); User user = this.GetUser(); userManager .Setup(um => um.FindByIdAsync(It.IsAny <string>())) .ReturnsAsync(user); userManager .Setup(um => um.GetRolesAsync(user)) .ReturnsAsync(new List <string>() { WebConstants.AdministratorRole }); return(userManager); }