public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(randomDate); CalendarEntry invalidCalendarEntry = randomCalendarEntry; invalidCalendarEntry.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); CalendarEntry storageCalendarEntry = randomCalendarEntry.DeepClone(); Guid calendarEntryId = invalidCalendarEntry.Id; invalidCalendarEntry.CreatedBy = invalidCreatedBy; var invalidCalendarEntryInputException = new InvalidCalendarEntryException( parameterName: nameof(CalendarEntry.CreatedBy), parameterValue: invalidCalendarEntry.CreatedBy); var expectedCalendarEntryValidationException = new CalendarEntryValidationException(invalidCalendarEntryInputException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(calendarEntryId)) .ReturnsAsync(storageCalendarEntry); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <CalendarEntry> modifyCalendarEntryTask = this.calendarEntryService.ModifyCalendarEntryAsync(invalidCalendarEntry); // then await Assert.ThrowsAsync <CalendarEntryValidationException>(() => modifyCalendarEntryTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(invalidCalendarEntry.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 ShouldModifyCalendarEntryAsync() { // given DateTimeOffset randomDate = GetRandomDateTime(); DateTimeOffset randomInputDate = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(randomInputDate); CalendarEntry inputCalendarEntry = randomCalendarEntry; CalendarEntry afterUpdateStorageCalendarEntry = inputCalendarEntry; CalendarEntry expectedCalendarEntry = afterUpdateStorageCalendarEntry; CalendarEntry beforeUpdateStorageCalendarEntry = randomCalendarEntry.DeepClone(); inputCalendarEntry.UpdatedDate = randomDate; Guid calendarEntryId = inputCalendarEntry.Id; this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(calendarEntryId)) .ReturnsAsync(beforeUpdateStorageCalendarEntry); this.storageBrokerMock.Setup(broker => broker.UpdateCalendarEntryAsync(inputCalendarEntry)) .ReturnsAsync(afterUpdateStorageCalendarEntry); // when CalendarEntry actualCalendarEntry = await this.calendarEntryService.ModifyCalendarEntryAsync(inputCalendarEntry); // then actualCalendarEntry.Should().BeEquivalentTo(expectedCalendarEntry); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(calendarEntryId), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateCalendarEntryAsync(inputCalendarEntry), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }