public async Task Handle_WhenUserDoesNotHavePermission_ShouldThrowException() { // Arrange var userId = Guid.NewGuid(); var command = new CreateTodoCommand { UserId = userId, Name = "todo one", IsComplete = true }; var rpcResponse = new IsUserAllowedResponse { IsAllowed = false }; rpcPermissionsMock.ClientMock.Setup(x => x.IsUserAllowed(It.IsAny <IsUserAllowedRequest>(), It.IsAny <CallOptions>())).Returns(rpcResponse); var dbOptions = new DbContextOptionsBuilder <TodoDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (var context = new TodoDbContext(dbOptions)) { // Act var sut = new CreateTodoCommandHandler(context, mapper, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient); try { var result = await sut.Handle(command, new CancellationToken(false)); Assert.True(false, "Should throw exception"); } catch (InvalidOperationException ex) { // Assert Assert.NotNull(ex); } } }
public async Task Handle_WhenUserHasPermission_ShouldCreateTodo() { // Arrange var userId = Guid.NewGuid(); var command = new CreateTodoCommand { UserId = userId, Name = "todo one", IsComplete = true }; var dbOptions = new DbContextOptionsBuilder <TodoDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; long todoId = 0; using (var context = new TodoDbContext(dbOptions)) { var sut = new CreateTodoCommandHandler(context, mapper, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient); // Act todoId = await sut.Handle(command, new CancellationToken(false)); // Assert todoId.ShouldBeGreaterThan(0); var createdTodo = await context.Todos.FirstOrDefaultAsync(t => t.Id == todoId); createdTodo.ShouldNotBeNull(); createdTodo.Name.ShouldBe(command.Name); createdTodo.IsComplete.ShouldBe(command.IsComplete); } }
public async Task CreateTdDoCommandHandlerTestShouldReturnGenericCommandResultObject() { var todo = new TodoItem("Teste", false, DateTime.Now, "Teste"); var command = new CreateTodoCommand(); var handler = new CreateTodoCommandHandler(_toDoRepositoryMock.Object); _toDoRepositoryMock.Setup(a => a.Add(todo)).ReturnsAsync(todo); //Act var result = await((IRequestHandler <CreateTodoCommand, GenericCommandResult>)handler).Handle(command, new CancellationToken()); result.Success.Should().BeTrue(); result.Message.Should().Be("Tarefa salva"); }
public async Task Handle_WhenTodoCreated_ShouldNotifiyUser() { // Arrange var userId = Guid.NewGuid(); var command = new CreateTodoCommand { UserId = userId, Name = "todo one", IsComplete = true }; var dbOptions = new DbContextOptionsBuilder <TodoDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (var context = new TodoDbContext(dbOptions)) { var sut = new CreateTodoCommandHandler(context, mapper, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient); // Act await sut.Handle(command, new CancellationToken(false)); } // Assert rpcNotificationsMock.ClientMock.Verify(x => x.SendEmail(It.IsAny <SendEmailRequest>(), It.IsAny <CallOptions>()), Times.Once); }
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); }