public void DeleteAsyncSuccessTest() { // 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>()).Returns(true); unitOfWork.CompleteAsync().Returns(Task.CompletedTask); QbQuestionService service = new QbQuestionService(repository, unitOfWork); // Act Task <QbQuestionResponse> result = service.DeleteAsync(1); // Assert result.Result.Success.Should().Be(true); }
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); }
public async Task <QbQuestionResponse> DeleteAsync(int id) { QbQuestion oldQuestion = await _qbQuestionRepository.FindByIdAsync(id); if (oldQuestion == null) { return(new QbQuestionResponse("Question not found.")); } try { _qbQuestionRepository.Remove(oldQuestion); await _unitOfWork.CompleteAsync(); return(new QbQuestionResponse(oldQuestion)); } catch (Exception ex) { return(new QbQuestionResponse($"An error occurred when deleting the question: {ex.Message}")); } }