public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedCalendarServiceException = new CalendarServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllCalendars()) .Throws(exception); // when . then Assert.Throws <CalendarServiceException>(() => this.calendarService.RetrieveAllCalendars()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCalendarServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllCalendars(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomCalendarId = Guid.NewGuid(); Guid inputCalendarId = randomCalendarId; var exception = new Exception(); var expectedCalendarServiceException = new CalendarServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectCalendarByIdAsync(inputCalendarId)) .ThrowsAsync(exception); // when ValueTask <Calendar> deleteCalendarTask = this.calendarService.RemoveCalendarByIdAsync(inputCalendarId); // then await Assert.ThrowsAsync <CalendarServiceException>(() => deleteCalendarTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCalendarServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarByIdAsync(inputCalendarId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private CalendarServiceException CreateAndLogServiceException(Exception exception) { var calendarServiceException = new CalendarServiceException(exception); this.loggingBroker.LogError(calendarServiceException); return(calendarServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Calendar randomCalendar = CreateRandomCalendar(randomDateTime); Calendar someCalendar = randomCalendar; someCalendar.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedCalendarServiceException = new FailedCalendarServiceException(serviceException); var expectedCalendarServiceException = new CalendarServiceException(failedCalendarServiceException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarByIdAsync(someCalendar.Id)) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Calendar> modifyCalendarTask = this.calendarService.ModifyCalendarAsync(someCalendar); // then await Assert.ThrowsAsync <CalendarServiceException>(() => modifyCalendarTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarByIdAsync(someCalendar.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Calendar randomCalendar = CreateRandomCalendar(dateTime); Calendar inputCalendar = randomCalendar; inputCalendar.UpdatedBy = inputCalendar.CreatedBy; var serviceException = new Exception(); var failedCalendarServiceException = new FailedCalendarServiceException(serviceException); var expectedCalendarServiceException = new CalendarServiceException(failedCalendarServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCalendarAsync(inputCalendar)) .ThrowsAsync(serviceException); // when ValueTask <Calendar> registerCalendarTask = this.calendarService.AddCalendarAsync(inputCalendar); // then await Assert.ThrowsAsync <CalendarServiceException>(() => registerCalendarTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCalendarAsync(inputCalendar), 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 someCalendarId = Guid.NewGuid(); var serviceException = new Exception(); var failedCalendarServiceException = new FailedCalendarServiceException(serviceException); var expectedCalendarServiceException = new CalendarServiceException(failedCalendarServiceException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarByIdAsync(someCalendarId)) .ThrowsAsync(serviceException); // when ValueTask <Calendar> retrieveCalendarByIdTask = this.calendarService.RetrieveCalendarByIdAsync(someCalendarId); // then await Assert.ThrowsAsync <CalendarServiceException>(() => retrieveCalendarByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarByIdAsync(someCalendarId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }