public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllStudentSemesterCourses()) .Throws(exception); // when . then Assert.Throws <StudentSemesterCourseServiceException>(() => this.studentSemesterCourseService.RetrieveAllStudentSemesterCourses()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllStudentSemesterCourses(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private StudentSemesterCourseServiceException CreateAndLogServiceException(Exception exception) { var studentSemesterCourseServiceException = new StudentSemesterCourseServiceException(exception); this.loggingBroker.LogError(studentSemesterCourseServiceException); return(studentSemesterCourseServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(randomDateTime); StudentSemesterCourse someStudentSemesterCourse = randomStudentSemesterCourse; someStudentSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException( failedStudentSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync (It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentSemesterCourse> modifyStudentSemesterCourseTask = this.studentSemesterCourseService.ModifyStudentSemesterCourseAsync(someStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => modifyStudentSemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime); StudentSemesterCourse inputStudentSemesterCourse = randomStudentSemesterCourse; inputStudentSemesterCourse.UpdatedBy = inputStudentSemesterCourse.CreatedBy; inputStudentSemesterCourse.UpdatedDate = inputStudentSemesterCourse.CreatedDate; var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException(failedStudentSemesterCourseServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentSemesterCourseAsync(It.IsAny <StudentSemesterCourse>())) .ThrowsAsync(serviceException); // when ValueTask <StudentSemesterCourse> createStudentSemesterCourseTask = this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(inputStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => createStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentSemesterCourseAsync(It.IsAny <StudentSemesterCourse>()), 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(); Guid someStudentId = Guid.NewGuid(); var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException( failedStudentSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync(someStudentId, someSemesterCourseId)) .ThrowsAsync(serviceException); // when ValueTask <StudentSemesterCourse> deleteStudentSemesterCourseTask = this.studentSemesterCourseService.RetrieveStudentSemesterCourseByIdAsync (someStudentId, someSemesterCourseId); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => deleteStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(someStudentId, someSemesterCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }