public async Task <QbQuestionResponse> SaveAsync(QbQuestion qbQuestion) { try { await _qbQuestionRepository.AddAsync(qbQuestion); await _unitOfWork.CompleteAsync(); return(new QbQuestionResponse(qbQuestion)); } catch (Exception ex) { return(new QbQuestionResponse($"An error occurred when saving the question: {ex.Message}")); } }
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 SaveAsyncSuccessTest() { // Arrange IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>(); IUnitOfWork unitOfWork = Substitute.For <IUnitOfWork>(); repository.AddAsync(Arg.Any <QbQuestion>()).Returns(Task.CompletedTask); unitOfWork.CompleteAsync().Returns(Task.CompletedTask); QbQuestionService service = new QbQuestionService(repository, unitOfWork); QbQuestion question = new QbQuestion(); // Act Task <QbQuestionResponse> result = service.SaveAsync(question); // Assert QbQuestionResponse response = new QbQuestionResponse(question); result.Result.Success.Should().Be(true); }