public async Task Update_Is_Ok() { // Arrange var mockTodoService = new Mock <ITodoService>(); var mockTodoMapper = new Mock <ITodoModelDtoMapper>(); _ = mockTodoService.Setup(s => s.UpdateTodo(It.IsAny <Guid>(), It.IsAny <TodoDto>())) .ReturnsAsync(new TodoDto()); _ = mockTodoMapper.Setup(s => s.Map(It.IsAny <TodoRequestModel>())) .Returns(new TodoDto()); _ = mockTodoMapper.Setup(s => s.Map(It.IsAny <TodoDto>())) .Returns(new TodoResponseModel()); var controller = new TodosController(mockTodoService.Object, mockTodoMapper.Object); // Act var result = await controller.Update(It.IsAny <Guid>(), It.IsAny <TodoRequestModel>()); // Assert _ = result.Result.Should().BeOfType <OkObjectResult>(); var resultObject = result.GetObjectResult(); _ = resultObject.Should().BeOfType <TodoResponseModel>(); _ = resultObject.Should().NotBeNull(); mockTodoMapper.Verify(c => c.Map(It.IsAny <TodoRequestModel>()), Times.Once); mockTodoService.Verify(c => c.UpdateTodo(It.IsAny <Guid>(), It.IsAny <TodoDto>()), Times.Once); mockTodoMapper.Verify(c => c.Map(It.IsAny <TodoDto>()), Times.Once); }
public void Update_InvalidId_BadRequest() { //Arrange var controller = new TodosController(_todoItemService, null, null); var id = Guid.Empty; //Act var updatedData = controller.Update(id, TestHelper.TodoItems.ElementAt(1)); //Assert Assert.IsType <BadRequestObjectResult>(updatedData); }
public void Update_ValidData_OkResult() { //Arrange var controller = new TodosController(_todoItemService, null, null); var id = TestHelper.TodoItems.ElementAt(1).Id; //Act var existingItem = controller.Read(id); var okResult = (OkObjectResult)existingItem; var item = (TodoItem)okResult.Value; item.Name = "Updated Todo"; var updatedData = controller.Update(id, item); //Assert Assert.IsType <OkObjectResult>(updatedData); }
public void Update_InvalidData_BadRequest() { //Arrange var controller = new TodosController(_todoItemService, null, null); var id = TestHelper.TodoItems.ElementAt(1).Id; //Act var item = new TodoItem() { Name = null }; controller.ModelState.AddModelError("Name", "Name is required."); var updatedData = controller.Update(id, item); //Assert Assert.IsType <BadRequestObjectResult>(updatedData); }