public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedSemesterCourseServiceException = new SemesterCourseServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllSemesterCourses()) .Throws(exception); // when . then Assert.Throws <SemesterCourseServiceException>(() => this.semesterCourseService.RetrieveAllSemesterCourses()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllSemesterCourses(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomSemesterCourseId = Guid.NewGuid(); Guid inputSemesterCourseId = randomSemesterCourseId; var exception = new Exception(); var expectedSemesterCourseServiceException = new SemesterCourseServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId)) .ThrowsAsync(exception); // when ValueTask <SemesterCourse> deleteSemesterCourseTask = this.semesterCourseService.DeleteSemesterCourseAsync(inputSemesterCourseId); // then await Assert.ThrowsAsync <SemesterCourseServiceException>(() => deleteSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private SemesterCourseServiceException CreateAndLogServiceException(Exception exception) { var semesterCourseServiceException = new SemesterCourseServiceException(exception); this.loggingBroker.LogError(semesterCourseServiceException); return(semesterCourseServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(randomDateTime); SemesterCourse someSemesterCourse = randomSemesterCourse; someSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedSemesterCourseServiceException = new FailedSemesterCourseServiceException(serviceException); var expectedSemesterCourseServiceException = new SemesterCourseServiceException(failedSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id)) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <SemesterCourse> modifySemesterCourseTask = this.semesterCourseService.ModifySemesterCourseAsync(someSemesterCourse); // then await Assert.ThrowsAsync <SemesterCourseServiceException>(() => modifySemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(dateTime); SemesterCourse inputSemesterCourse = randomSemesterCourse; inputSemesterCourse.UpdatedBy = inputSemesterCourse.CreatedBy; inputSemesterCourse.UpdatedDate = inputSemesterCourse.CreatedDate; var serviceException = new Exception(); var failedSemesterCourseServiceException = new FailedSemesterCourseServiceException(serviceException); var expectedSemesterCourseServiceException = new SemesterCourseServiceException(failedSemesterCourseServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertSemesterCourseAsync(inputSemesterCourse)) .ThrowsAsync(serviceException); // when ValueTask <SemesterCourse> createSemesterCourseTask = this.semesterCourseService.CreateSemesterCourseAsync(inputSemesterCourse); // then await Assert.ThrowsAsync <SemesterCourseServiceException>(() => createSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertSemesterCourseAsync(inputSemesterCourse), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync() { // given Guid someSemesterCourseId = Guid.NewGuid(); var serviceException = new Exception(); var failedSemesterCourseServiceException = new FailedSemesterCourseServiceException(serviceException); var expectedSemesterCourseServiceException = new SemesterCourseServiceException(failedSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourseId)) .ThrowsAsync(serviceException); // when ValueTask <SemesterCourse> retrieveSemesterCourseByIdTask = this.semesterCourseService.RetrieveSemesterCourseByIdAsync(someSemesterCourseId); // then await Assert.ThrowsAsync <SemesterCourseServiceException>(() => retrieveSemesterCourseByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourseId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }