public async Task ReturnSuccess_ForMarkDone() { // Arrange var mockTodoItemService = Substitute.For <ITodoItemService>(); var mockUserManager = MockUserManager.Create(); // Make the mockUserManager return a fake user var fakeUser = new ApplicationUser(); mockUserManager.GetUserAsync(Arg.Any <ClaimsPrincipal>()) .Returns(Task.FromResult(fakeUser)); // Make the mockTodoItemService always succeed mockTodoItemService.MarkDoneAsync(Arg.Any <Guid>(), Arg.Any <ApplicationUser>()) .Returns(Task.FromResult(true)); var controller = new TodoController(mockTodoItemService, mockUserManager); // Act var randomId = Guid.NewGuid(); var result = await controller.MarkDone(randomId); var redirectResult = result as RedirectToActionResult; // Assert Assert.NotNull(redirectResult); Assert.Equal("Index", redirectResult.ActionName); }
public async Task MarkDone_ShouldReturn_RedirectToActionIndex() { // Arrange // Make the mockUserManager return a fake user var fakeUser = new IdentityUser(); mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(fakeUser)); // Make the mockTodoItemService always succeed mockTodoItemService .Setup(x => x.MarkDoneAsync(It.IsAny <Guid>(), It.IsAny <IdentityUser>())) .Returns(Task.FromResult(true)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var result = await controller.MarkDone(Guid.NewGuid()); var statusCodeResult = result as StatusCodeResult; // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal(nameof(TodoController.Index), redirectResult.ActionName); }
public async Task MarkDone_ShouldReturn_BadRequest_IfFailed() { // Arrange var fakeUser = new IdentityUser(); // Make the mockUserManager return a fake user mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(fakeUser)); // Make the mockTodoItemService always fails mockTodoItemService .Setup(x => x.MarkDoneAsync(It.IsAny <Guid>(), It.IsAny <IdentityUser>())) .Returns(Task.FromResult(false)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); //Act var result = await controller.MarkDone(Guid.NewGuid()); var objectResult = result as BadRequestObjectResult; // Assert Assert.NotNull(objectResult); Assert.IsType <string>(objectResult.Value); }
public async Task MarkDone_ShouldReturn_BadRequest_IfIdIsEmpty() { // Arrange var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var result = await controller.MarkDone(Guid.Empty); // Assert Assert.NotNull(result as BadRequestResult); }
public async Task MarkDone_ShouldReturn_ChallengeResult_IfUserIsUnauthorize() { // Arrange // Make the mockUserManager always return null for GetUserAsync mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(null)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var randomId = Guid.NewGuid(); var result = await controller.MarkDone(randomId); // Assert Assert.IsType <ChallengeResult>(result); }
public async Task ReturnBadRequest_ForMarkDoneWithBadId() { // Arrange var mockTodoItemService = Substitute.For <ITodoItemService>(); var mockUserManager = MockUserManager.Create(); var controller = new TodoController(mockTodoItemService, mockUserManager); // Act var result = await controller.MarkDone(Guid.Empty); var redirectResult = result as RedirectToActionResult; // Assert Assert.NotNull(redirectResult); Assert.Equal("Index", redirectResult.ActionName); }
public async Task ReturnBadRequest_ForMarkDoneWithBadId() { // Arrange var mockTodoItemService = Substitute.For <ITodoItemService>(); var mockUserManager = MockUserManager.Create(); var controller = new TodoController(mockTodoItemService, mockUserManager); // Act var result = await controller.MarkDone(Guid.Empty); var statusCodeResult = result as StatusCodeResult; // Assert Assert.NotNull(statusCodeResult); Assert.Equal(400, statusCodeResult.StatusCode); }
public async Task ReturnUnauthorized_ForMarkDoneWithNoUser() { // Arrange var mockTodoItemService = Substitute.For <ITodoItemService>(); var mockUserManager = MockUserManager.Create(); // Make the mockUserManager always return null for GetUserAsync mockUserManager.GetUserAsync(Arg.Any <ClaimsPrincipal>()) .Returns(Task.FromResult <ApplicationUser>(null)); var controller = new TodoController(mockTodoItemService, mockUserManager); // Act var randomId = Guid.NewGuid(); var result = await controller.MarkDone(randomId); var redirectResult = result as RedirectToActionResult; // Assert Assert.NotNull(redirectResult); Assert.Equal("Index", redirectResult.ActionName); }