Пример #1
0
        public async Task <QbQuestionResponse> UpdateAsync(int id, QbQuestion qbQuestion)
        {
            QbQuestion oldQuestion = await _qbQuestionRepository.FindByIdAsync(id);

            if (oldQuestion == null)
            {
                return(new QbQuestionResponse("Question not found."));
            }

            oldQuestion.Level      = qbQuestion.Level;
            oldQuestion.Tournament = qbQuestion.Tournament;
            oldQuestion.Year       = qbQuestion.Year;
            oldQuestion.Power      = qbQuestion.Power;
            oldQuestion.Body       = qbQuestion.Body;
            oldQuestion.Answer     = qbQuestion.Answer;
            oldQuestion.Notes      = qbQuestion.Notes;

            try
            {
                _qbQuestionRepository.Update(oldQuestion);
                await _unitOfWork.CompleteAsync();

                return(new QbQuestionResponse(oldQuestion));
            }
            catch (Exception ex)
            {
                return(new QbQuestionResponse($"An error occurred when updating the question: {ex.Message}"));
            }
        }
        public void UpdateAsyncSuccessTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();
            QbQuestion            question   = new QbQuestion();

            repository.FindByIdAsync(Arg.Any <int>()).Returns(question);
            repository.Update(Arg.Any <QbQuestion>()).Returns(true);
            unitOfWork.CompleteAsync().Returns(Task.CompletedTask);
            QbQuestionService service = new QbQuestionService(repository, unitOfWork);

            // Act
            Task <QbQuestionResponse> result = service.UpdateAsync(1, question);

            // Assert
            result.Result.Success.Should().Be(true);
        }
        public void UpdateAsyncFailureOnUpdateTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();
            QbQuestion            question   = new QbQuestion();

            repository.FindByIdAsync(Arg.Any <int>()).Returns(question);
            repository.Update(Arg.Any <QbQuestion>()).Throws(new Exception("Exception occurred on Update"));
            QbQuestionService service = new QbQuestionService(repository, unitOfWork);

            // Act
            Task <QbQuestionResponse> result = service.UpdateAsync(1, question);

            // Assert
            string errorMessage = "An error occurred when updating the question: Exception occurred on Update";

            result.Result.Success.Should().Be(false);
            result.Result.Message.Should().Be(errorMessage);
        }