public async Task WhenHandlingCommand_ThenGetTodoFromRepository() { // Arrange var todo = CreateTodo(); var request = Create(); todoRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(todo); // Act await sut.Handle(request, default); // Assert todoRepositoryMock.Verify(x => x.GetByIdAsync(request.Id), Times.Once); }
public async Task UpdateTodoCommandHandlerTestShouldReturnGenericCommandResultObject() { var command = new UpdateTodoCommand(Guid.NewGuid(), "Teste", "Teste"); var todo = new TodoItem("Teste", true, DateTime.Now, "Teste"); var handler = new UpdateTodoCommandHandler(_toDoRepositoryMock.Object); _toDoRepositoryMock.Setup(a => a.GetById(command.Id)).ReturnsAsync(todo); _toDoRepositoryMock.Setup(a => a.Update(todo)).ReturnsAsync(todo); //Act var result = await handler.Handle(command, new CancellationToken()); result.Success.Should().BeTrue(); result.Message.Should().Be("Tarefa salva"); }
private static void WorkWithCommandsWithoutIoc() { System.Console.WriteLine("\n-- No IoC --"); // Line up some commands to execute var itemToCreateThenChange = new Todo { Caption = "Feed cat", Category = "Etc" }; var cmd1 = new CreateTodoCommand { Data = new Todo { Caption = "I SHOULD BE DELETED", Category = "Etc" } }; // id will become 1 var cmd2 = new CreateTodoCommand { Data = itemToCreateThenChange }; // id will become 2 var cmd3 = new CreateTodoCommand { Data = new Todo { Caption = "Pay bills", Category = "Etc" } }; // id will become 3 var cmd4 = new CreateTodoCommand { Data = new Todo { Caption = "I DO NOT PASS THE FILTER", Category = "Uncategorized" } }; // id will become 3 var updateCommand = new UpdateTodoCommand { Data = new Todo { Id = itemToCreateThenChange.Id, Category = itemToCreateThenChange.Category, Caption = "Feed BOTH cats" } }; var deleteCommand = new DeleteTodoCommand { Id = 1 }; // delete milk // Prepare the handlers var createHandler = new CreateTodoCommandHandler(new CachedRepository <Todo>()); var updateHandler = new UpdateTodoCommandHandler(new CachedRepository <Todo>()); var deleteHandler = new DeleteTodoCommandHandler(new CachedRepository <Todo>()); // Example using a decorator. We're instantiating the decorator manually here but the idea is to it with IoC. // This decorator outputs some logging to to Console.Out. var decorating_deleteHandler = new LoggingCommandHandlerDecorator <DeleteTodoCommand>(deleteHandler, (ILogger) new ConsoleLogger()); // Create something then queue it for deletion createHandler.Handle(cmd1).Wait(); deleteCommand.Id = cmd1.Output; Task.WaitAll( createHandler.Handle(cmd2) , createHandler.Handle(cmd3) , createHandler.Handle(cmd4) ); updateHandler.Handle(updateCommand).Wait(); decorating_deleteHandler.Handle(deleteCommand).Wait(); // Example using a queryhandler to deliver results var qhandler = new FindTodosByCategoryQueryHandler(new CachedRepository <Todo>()); var query = new FindTodosByCategoryQuery { Category = "Etc" }; var queryResult = qhandler.Handle(query); // Only two items should show up since we deleted one, and one doesn't match the query. PrintResults(queryResult.Result); }