コード例 #1
0
        public async Task Handle_WhenRequestIsValid_ShouldReturnCommandResultSuccess()
        {
            //Arrange
            var successfulValidationResult = new ValidationResult();

            _validator.ValidateAsync(Arg.Any <MarkAsDoneTodoListItemCommand>()).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 MarkAsDoneTodoListItemCommand
            {
                TodoListItemId = todoListItemId
            };
            subject = new MarkAsDoneTodoListItemCommandHandler(_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 done successfully");
        }
コード例 #2
0
        public async Task <TodoItemDto> Handle(GetTodoListItemQuery request, CancellationToken cancellationToken)
        {
            var query = await _todoListItemsRepository.GetTodoListItemsByIdAsync(request.TodoListItemId, cancellationToken);

            var results = _mapper
                          .Map <TodoItemDto>(query);

            return(results);
        }
コード例 #3
0
        public async Task <ICommandResult <Unit> > Handle(DeleteTodoListItemCommand 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);

            _todoListItemsRepository.Remove(todoListItem);

            await _todoListItemsRepository.SaveChangesAsync(cancellationToken);

            return(CommandResult <Unit> .Success(Unit.Value, "The to-do item was deleted successfully"));
        }
コード例 #4
0
        public async Task Handle_WhenRequestIsValid_ShouldReturnTodoItemDto()
        {
            //Arrange

            var todoListItem = TodoListItemTestData.CreateTodoListItemTestData();

            var todoListItemId = todoListItem.TodoListItemId;

            _todoListItemsRepository.GetTodoListItemsByIdAsync(Arg.Any <Guid>()).Returns(todoListItem);

            request = new GetTodoListItemQuery
            {
                TodoListItemId = todoListItemId,
            };

            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new TodoListsProfile());
            });

            _mapper = mappingConfig.CreateMapper();

            subject = new GetTodoListItemQueryHandler(_todoListItemsRepository, _mapper);
            //Act
            var result = await subject.Handle(request, default);

            //Assert

            var querResult = new TodoItemDto
            {
                TodoListItemId = todoListItem.TodoListItemId,
                Name           = todoListItem.Name,
                Description    = todoListItem.Description,
                DueDate        = todoListItem.DueDate,
                CompletedOn    = todoListItem.CompletedOn,
                StatusName     = todoListItem.Status == TodoListItemStatuses.Pending ? "Pending" : "Done"
            };

            result.ShouldBeOfType <TodoItemDto>();
            result.ShouldBeEquivalentTo(querResult);
        }