public async Task Get_ReturnsOkAndToDo_WhenServiceReturnsToDo() { // Arrange var mockServiceResponse = new BusinessModels.Todo() { Id = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"), Name = "ToDo1" }; _todoServiceMock.Setup(s => s.GetTodo(It.IsAny <Guid>())).ReturnsAsync(mockServiceResponse); // We dont care to specify Id, we don't test the logic of the service/repository in controller tests. var unitUnderTest = new ToDoController(_mapper, _todoServiceMock.Object); var expectedControllerResponse = new ApiModels.TodoModel() { Id = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"), Name = "ToDo1" }; // Act var response = await unitUnderTest.Get(Guid.NewGuid()); // We dont care to specify Id, we don't test the logic of the service/repository in controller tests. // Assert var okObjectResult = response as OkObjectResult; okObjectResult.Should().NotBe(null); okObjectResult.StatusCode.Should().Be((int)HttpStatusCode.OK); var todoReturned = okObjectResult.Value as ApiModels.TodoModel; todoReturned.Should().BeEquivalentTo(expectedControllerResponse); }
public async Task AddTodo(BusinessModels.Todo newTodo) { // Map the BusinessLayer model to database model var newTodoEntity = _mapper.Map <BusinessModels.Todo, RepositoryModels.TodoEntity>(newTodo); // Add to database, Wrapping in Task.FromResult( result ) to fake database async call. _anInMemoryDatabaseOfTodos.Add(newTodoEntity); }
public async Task UpdateTodo(Guid todoId, BusinessModels.Todo updatedToDo) { // Wrapping in Task.FromResult( result ) to fake database async call. var todoFromDatabase = await Task.FromResult(_anInMemoryDatabaseOfTodos.Where(td => td.Id == todoId).FirstOrDefault()); // If exists update to the new name. if (todoFromDatabase != null) { todoFromDatabase.Name = updatedToDo.Name; } }