示例#1
0
        public async Task CreateTodoAsync_MakesCorrectCallsAndReturnsUpdatedTodoWithId()
        {
            // Arrange
            var expectedCreatedTodoDTO = new CreatedTodoDTO()
            {
                Name = "Test Todo", IsComplete = true
            };
            var expectedTodoDTO = new TodoDTO()
            {
                Name = expectedCreatedTodoDTO.Name, IsComplete = expectedCreatedTodoDTO.IsComplete.Value
            };
            var expectedPrimaryKeyId = 1;
            var todo             = new Todo();
            var mockedMapper     = new Mock <IMapper>();
            var mockedRepository = new Mock <ITodosRepository>();

            mockedMapper.Setup(m => m.Map <CreatedTodoDTO, Todo>(expectedCreatedTodoDTO)).Returns(todo);
            mockedMapper.Setup(m => m.Map <CreatedTodoDTO, TodoDTO>(expectedCreatedTodoDTO)).Returns(expectedTodoDTO);
            mockedRepository.Setup(r => r.CreateTodoAsync(todo)).ReturnsAsync(expectedPrimaryKeyId);

            var service = new TodosService(mockedMapper.Object, mockedRepository.Object);

            // Action
            var actualTodoDTO = await service.CreateTodoAsync(expectedCreatedTodoDTO);

            // Assert
            Assert.NotNull(actualTodoDTO);
            Assert.Equal(expectedPrimaryKeyId, actualTodoDTO.Id);
            Assert.Equal(expectedTodoDTO.Name, actualTodoDTO.Name);
            Assert.Equal(expectedTodoDTO.IsComplete, actualTodoDTO.IsComplete);

            mockedMapper.Verify(m => m.Map <CreatedTodoDTO, Todo>(expectedCreatedTodoDTO), Times.Once());
            mockedMapper.Verify(m => m.Map <CreatedTodoDTO, TodoDTO>(expectedCreatedTodoDTO), Times.Once());
            mockedRepository.Verify(r => r.CreateTodoAsync(todo), Times.Once());
        }
示例#2
0
        public async Task <ActionResult> CreateTodo(TodoCreateModel model)
        {
            var newRecord = await _todosService.CreateTodoAsync(model);

            return(CreatedAtRoute(routeName: nameof(GetTodoById), routeValues: new { newRecord.Id }, value: newRecord));
        }