public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomExamId = Guid.NewGuid(); Guid inputExamId = randomExamId; var exception = new Exception(); var expectedExamServiceException = new ExamServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectExamByIdAsync(inputExamId)) .ThrowsAsync(exception); // when ValueTask <Exam> deleteExamTask = this.examService.RemoveExamByIdAsync(inputExamId); // then await Assert.ThrowsAsync <ExamServiceException>(() => deleteExamTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectExamByIdAsync(inputExamId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedExamServiceException = new ExamServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllExams()) .Throws(exception); // when . then Assert.Throws <ExamServiceException>(() => this.examService.RetrieveAllExams()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllExams(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private ExamServiceException CreateAndLogServiceException(Exception exception) { var examServiceException = new ExamServiceException(exception); this.loggingBroker.LogError(examServiceException); return(examServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Exam randomExam = CreateRandomExam(randomDateTime); Exam someExam = randomExam; someExam.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var expectedExamServiceException = new ExamServiceException(serviceException); this.storageBrokerMock.Setup(broker => broker.SelectExamByIdAsync(someExam.Id)) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Exam> modifyExamTask = this.examService.ModifyExamAsync(someExam); // then await Assert.ThrowsAsync <ExamServiceException>(() => modifyExamTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectExamByIdAsync(someExam.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Exam randomExam = CreateRandomExam(dateTime); Exam inputExam = randomExam; inputExam.UpdatedBy = inputExam.CreatedBy; var exception = new Exception(); var expectedExamServiceException = new ExamServiceException(exception); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertExamAsync(inputExam)) .ThrowsAsync(exception); // when ValueTask <Exam> createExamTask = this.examService.AddExamAsync(inputExam); // then await Assert.ThrowsAsync <ExamServiceException>(() => createExamTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertExamAsync(inputExam), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLogItAsync() { // given Exam someExam = CreateRandomExam(); var serviceException = new Exception(); var failedExamServiceException = new FailedExamServiceException(serviceException); var expectedExamServiceException = new ExamServiceException(failedExamServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Throws(serviceException); // when ValueTask <Exam> modifyExamTask = this.examService.ModifyExamAsync(someExam); // then await Assert.ThrowsAsync <ExamServiceException>(() => modifyExamTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectExamByIdAsync(someExam.Id), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() { // given Guid someExamId = Guid.NewGuid(); var serviceException = new Exception(); var failedExamServiceException = new FailedExamServiceException(serviceException); var expectedExamServiceException = new ExamServiceException(failedExamServiceException); this.storageBrokerMock.Setup(broker => broker.SelectExamByIdAsync(someExamId)) .ThrowsAsync(serviceException); // when ValueTask <Exam> retrieveExamByIdTask = this.examService.RetrieveExamByIdAsync(someExamId); // then await Assert.ThrowsAsync <ExamServiceException>(() => retrieveExamByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectExamByIdAsync(someExamId), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogIt() { // given var serviceException = new Exception(); var failedExamServiceException = new FailedExamServiceException(serviceException); var expectedExamServiceException = new ExamServiceException(failedExamServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllExams()) .Throws(serviceException); // when Action retrieveAllExamsAction = () => this.examService.RetrieveAllExams(); // then Assert.Throws <ExamServiceException>( retrieveAllExamsAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllExams(), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }