public async Task Handle_WhenRequestIsValid_ShouldReturnCommandResultSuccess() { //Arrange var successfulValidationResult = new ValidationResult(); _validator.ValidateAsync(Arg.Any <UpdateTodoListItemCommand>()).Returns(successfulValidationResult); var todoListItem = TodoListItemTestData.CreateTodoListItemTestData(); var todoListItemId = todoListItem.TodoListItemId; _todoListItemsRepository.AnyAsync(Arg.Any <Expression <Func <TodoListItem, bool> > >()).Returns(true); _todoListItemsRepository.GetTodoListItemsByIdAsync(Arg.Any <Guid>()).Returns(todoListItem); request = new UpdateTodoListItemCommand { TodoListItemId = todoListItemId, Name = "New Name", Description = "New Description", DueDate = DateTime.Now }; subject = new UpdateTodoListItemCommandHandler(_todoListItemsRepository, _validator); //Act var result = await subject.Handle(request, default); //Assert result.Errors.ShouldBeEmpty(); result.IsSuccessful.ShouldBeTrue(); result.Result.ShouldBeOfType <MediatR.Unit>(); result.Message.ShouldBe("The to-do item was updated successfully"); }
public TodoListItemsController( CreateTodoListItemCommand createTodoListItemCommand, UpdateTodoListItemCommand updateTodoListItemCommand, RemoveTodoListItemCommand removeTodoListItemCommand ) { this.createTodoListItemCommand = createTodoListItemCommand; this.updateTodoListItemCommand = updateTodoListItemCommand; this.removeTodoListItemCommand = removeTodoListItemCommand; }
public async Task <ICommandResult <Unit> > Handle(UpdateTodoListItemCommand request, CancellationToken cancellationToken) { var validationResult = await _validator.ValidateAsync(request, cancellationToken); if (!validationResult.IsValid) { return(CommandResult <Unit> .Failure(validationResult.Errors)); } var todoListItem = await _todoListItemsRepository .GetTodoListItemsByIdAsync(request.TodoListItemId, cancellationToken); todoListItem.Update(request.Name, request.Description, request.DueDate); await _todoListItemsRepository.SaveChangesAsync(cancellationToken); return(CommandResult <Unit> .Success(Unit.Value, "The to-do item was updated successfully")); }
public async Task Handle_WhenValidationFails_ShouldReturnCommandResultFailure() { //Arrange var validationErrors = new List <ValidationFailure> { new ValidationFailure("PropertyName", "A validation error message"), new ValidationFailure("PropertyName2", "Another validation error message") }; var successfulValidationResult = new ValidationResult(validationErrors); _validator.ValidateAsync(Arg.Any <UpdateTodoListItemCommand>()).Returns(new ValidationResult(validationErrors)); request = new UpdateTodoListItemCommand(); subject = new UpdateTodoListItemCommandHandler(_todoListItemsRepository, _validator); //Act var result = await subject.Handle(request, default); //Assert result.Errors.ShouldBe(validationErrors.Select(error => error.ErrorMessage)); result.IsSuccessful.ShouldBeFalse(); result.Result.ShouldBeOfType <MediatR.Unit>(); result.Message.ShouldBeNull(); }
public async Task<ActionResult> UpdateTodoListItem([FromBody] UpdateTodoListItemCommand request) { var result = await _mediator.Send(request); return Ok(result); }