public async Task ShouldThrowValidationExceptionOnModifyIfCalendarEntryDoesntExistAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); DateTimeOffset dateTime = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(dateTime); CalendarEntry nonExistentCalendarEntry = randomCalendarEntry; nonExistentCalendarEntry.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); CalendarEntry noCalendarEntry = null; var notFoundCalendarEntryException = new NotFoundCalendarEntryException(nonExistentCalendarEntry.Id); var expectedCalendarEntryValidationException = new CalendarEntryValidationException(notFoundCalendarEntryException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(nonExistentCalendarEntry.Id)) .ReturnsAsync(noCalendarEntry); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <CalendarEntry> modifyCalendarEntryTask = this.calendarEntryService.ModifyCalendarEntryAsync(nonExistentCalendarEntry); // then await Assert.ThrowsAsync <CalendarEntryValidationException>(() => modifyCalendarEntryTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(nonExistentCalendarEntry.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnRemoveWhenStorageCalendarEntryIsInvalidAndLogItAsync() { // given Guid randomCalendarEntryId = Guid.NewGuid(); Guid inputCalendarEntryId = randomCalendarEntryId; CalendarEntry invalidStorageCalendarEntry = null; var notFoundCalendarEntryException = new NotFoundCalendarEntryException(inputCalendarEntryId); var expectedCalendarEntryValidationException = new CalendarEntryValidationException(notFoundCalendarEntryException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId)) .ReturnsAsync(invalidStorageCalendarEntry); // when ValueTask <CalendarEntry> deleteCalendarEntryByIdTask = this.calendarEntryService.RemoveCalendarEntryByIdAsync(inputCalendarEntryId); // then await Assert.ThrowsAsync <CalendarEntryValidationException>(() => deleteCalendarEntryByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteCalendarEntryAsync(It.IsAny <CalendarEntry>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }