Exemplo n.º 1
0
        public async Task AddItemPost_ShouldReturn_BadRequest_IfFailed()
        {
            // Arrange
            var fakeUser = new IdentityUser();

            // Make the mockUserManager always return a fake user for GetUserAsync
            mockUserManager
            .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult <IdentityUser>(fakeUser));

            // Make the mockTodoItemService always fails
            mockTodoItemService
            .Setup(x => x.AddItemAsync(It.IsAny <TodoItem>(), It.IsAny <IdentityUser>()))
            .Returns(Task.FromResult(false));

            var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object);
            var model      = new TodoItemAddViewModel();

            //Act
            var result = await controller.AddItem(model);

            var objectResult = result as BadRequestObjectResult;

            // Assert
            Assert.NotNull(objectResult);
            Assert.IsType <string>(objectResult.Value);
        }
Exemplo n.º 2
0
        public async Task AddItemPost_ShouldReturn_RedirectToActionIndex_IfModelIsValid()
        {
            // 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 succeed
            mockTodoItemService
            .Setup(x => x.AddItemAsync(It.IsAny <TodoItem>(), It.IsAny <IdentityUser>()))
            .Returns(Task.FromResult(true));

            var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object);
            var model      = new TodoItemAddViewModel();

            //Act
            var result = await controller.AddItem(model);

            var objectResult = result as RedirectToActionResult;

            // Assert
            Assert.NotNull(objectResult);
            Assert.Equal(nameof(TodoController.Index), objectResult.ActionName);
        }
Exemplo n.º 3
0
        public async Task AddItemGet_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 result = await controller.AddItem();

            // Assert
            Assert.IsType <ChallengeResult>(result);
        }
Exemplo n.º 4
0
        public async Task AddItemPost_ShouldReturn_BadRequest_IfModelIsInvalid()
        {
            // Arrange
            var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object);
            var model      = new TodoItemAddViewModel();

            controller.ModelState.AddModelError("error", "testerror");

            //Act
            var result = await controller.AddItem(model);

            var objectResult = result as BadRequestObjectResult;

            // Assert
            Assert.NotNull(objectResult);
            Assert.IsType <SerializableError>(objectResult.Value);
        }
Exemplo n.º 5
0
        public async Task AddItemGet_ShouldReturn_ViewResult_TodoItemAddViewModel()
        {
            // Arrange
            // Make the mockUserManager always return fake user for GetUserAsync
            var fakeUser = new IdentityUser();

            mockUserManager
            .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult <IdentityUser>(fakeUser));
            var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object);

            // Act
            var result = await controller.AddItem();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <TodoItemAddViewModel>(viewResult.ViewData.Model);
        }