public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(randomDate); Attendance invalidAttendance = randomAttendance; invalidAttendance.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); Attendance storageAttendance = randomAttendance.DeepClone(); Guid attendanceId = invalidAttendance.Id; invalidAttendance.CreatedBy = invalidCreatedBy; var invalidAttendanceException = new InvalidAttendanceException( parameterName: nameof(Attendance.CreatedBy), parameterValue: invalidAttendance.CreatedBy); var expectedAttendanceValidationException = new AttendanceValidationException(invalidAttendanceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(attendanceId)) .ReturnsAsync(storageAttendance); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <Attendance> modifyAttendanceTask = this.attendanceService.ModifyAttendanceAsync(invalidAttendance); // then await Assert.ThrowsAsync <AttendanceValidationException>(() => modifyAttendanceTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(invalidAttendance.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldModifyAttendanceAsync() { // given DateTimeOffset randomDate = GetRandomDateTime(); DateTimeOffset randomInputDate = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(randomInputDate); Attendance inputAttendance = randomAttendance; Attendance afterUpdateStorageAttendance = inputAttendance; Attendance expectedAttendance = afterUpdateStorageAttendance; Attendance beforeUpdateStorageAttendance = randomAttendance.DeepClone(); inputAttendance.UpdatedDate = randomDate; Guid attendanceId = inputAttendance.Id; this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(attendanceId)) .ReturnsAsync(beforeUpdateStorageAttendance); this.storageBrokerMock.Setup(broker => broker.UpdateAttendanceAsync(inputAttendance)) .ReturnsAsync(afterUpdateStorageAttendance); // when Attendance actualAttendance = await this.attendanceService.ModifyAttendanceAsync(inputAttendance); // then actualAttendance.Should().BeEquivalentTo(expectedAttendance); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(attendanceId), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateAttendanceAsync(inputAttendance), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenUpdatedDateIsNotSameToCreatedDateAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(dateTime); Attendance inputAttendance = randomAttendance.DeepClone(); inputAttendance.UpdatedBy = randomAttendance.CreatedBy; inputAttendance.UpdatedDate = GetRandomDateTime(); var invalidAttendanceException = new InvalidAttendanceException(); invalidAttendanceException.AddData( key: nameof(Attendance.UpdatedDate), values: $"Date is not the same as {nameof(Attendance.CreatedDate)}"); var expectedAttendanceValidationException = new AttendanceValidationException(invalidAttendanceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Attendance> createAttendanceTask = this.attendanceService.CreateAttendanceAsync(inputAttendance); // then await Assert.ThrowsAsync <AttendanceValidationException>(() => createAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAttendanceAsync(It.IsAny <Attendance>()), Times.Never); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.AtLeastOnce); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }