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 SaveAsyncFailureOnAddTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();

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

            // Act
            Task <QbQuestionResponse> result = service.SaveAsync(question);

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

            result.Result.Success.Should().Be(false);
            result.Result.Message.Should().Be(errorMessage);
        }
        public void DeleteAsyncFailureOnFindTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();
            QbQuestion            question   = null;

            repository.FindByIdAsync(Arg.Any <int>()).Returns(question);
            QbQuestionService service = new QbQuestionService(repository, unitOfWork);

            // Act
            Task <QbQuestionResponse> result = service.DeleteAsync(1);

            // Assert
            string errorMessage = "Question not found.";

            result.Result.Success.Should().Be(false);
            result.Result.Message.Should().Be(errorMessage);
        }
        public void DeleteAsyncFailureOnRemoveTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();
            QbQuestion            question   = new QbQuestion();

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

            // Act
            Task <QbQuestionResponse> result = service.DeleteAsync(1);

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

            result.Result.Success.Should().Be(false);
            result.Result.Message.Should().Be(errorMessage);
        }
Пример #5
0
 public QbQuestionService(IQbQuestionRepository qbQuestionRepository, IUnitOfWork unitOfWork)
 {
     _qbQuestionRepository = qbQuestionRepository;
     _unitOfWork           = unitOfWork;
 }