public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomCourseId = Guid.NewGuid(); Guid inputCourseId = randomCourseId; var exception = new Exception(); var expectedCourseServiceException = new CourseServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectCourseByIdAsync(inputCourseId)) .ThrowsAsync(exception); // when ValueTask <Course> deleteCourseTask = this.courseService.DeleteCourseAsync(inputCourseId); // then await Assert.ThrowsAsync <CourseServiceException>(() => deleteCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(inputCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedCourseServiceException = new CourseServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllCourses()) .Throws(exception); // when . then Assert.Throws <CourseServiceException>(() => this.courseService.RetrieveAllCourses()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllCourses(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync() { // given var serviceException = new Exception(); var failedCourseServiceExcption = new FailedCourseServiceException(serviceException); var expectedCourseServiceException = new CourseServiceException(failedCourseServiceExcption); this.apiBrokerMock.Setup(broker => broker.GetAllCoursesAsync()) .ThrowsAsync(serviceException); // when ValueTask <List <Course> > retrieveAllCoursesTask = this.courseService.RetrieveAllCoursesAsync(); // then await Assert.ThrowsAsync <CourseServiceException>(() => retrieveAllCoursesTask.AsTask()); this.apiBrokerMock.Verify(broker => broker.GetAllCoursesAsync(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseServiceException))), Times.Once); this.apiBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private CourseServiceException CreateAndLogServiceException(Exception exception) { var courseServiceException = new CourseServiceException(exception); this.loggingBroker.LogError(courseServiceException); return(courseServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(randomDateTime); Course someCourse = randomCourse; someCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedCourseServiceException = new FailedCourseServiceException(serviceException); var expectedCourseServiceException = new CourseServiceException(failedCourseServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Throws(serviceException); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(someCourse); // then await Assert.ThrowsAsync <CourseServiceException>(() => modifyCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(dateTime); Course inputCourse = randomCourse; inputCourse.UpdatedBy = inputCourse.CreatedBy; var exception = new Exception(); var expectedCourseServiceException = new CourseServiceException(exception); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCourseAsync(inputCourse)) .ThrowsAsync(exception); // when ValueTask <Course> createCourseTask = this.courseService.CreateCourseAsync(inputCourse); // then await Assert.ThrowsAsync <CourseServiceException>(() => createCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCourseAsync(inputCourse), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync() { // given Guid someCourseId = Guid.NewGuid(); var serviceException = new Exception(); var failedCourseServiceException = new FailedCourseServiceException(serviceException); var expectedCourseServiceException = new CourseServiceException(failedCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(serviceException); // when ValueTask <Course> retrieveByIdCourseTask = this.courseService.RetrieveCourseByIdAsync(someCourseId); // then await Assert.ThrowsAsync <CourseServiceException>(() => retrieveByIdCourseTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }