public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); Classroom randomClassroom = CreateRandomClassroom(randomDate); Classroom invalidClassroom = randomClassroom; invalidClassroom.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); Classroom storageClassroom = randomClassroom.DeepClone(); Guid classroomId = invalidClassroom.Id; invalidClassroom.CreatedBy = invalidCreatedBy; var invalidClassroomInputException = new InvalidClassroomException( parameterName: nameof(Classroom.CreatedBy), parameterValue: invalidClassroom.CreatedBy); var expectedClassroomValidationException = new ClassroomValidationException(invalidClassroomInputException); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(classroomId)) .ReturnsAsync(storageClassroom); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <Classroom> modifyClassroomTask = this.classroomService.ModifyClassroomAsync(invalidClassroom); // then await Assert.ThrowsAsync <ClassroomValidationException>(() => modifyClassroomTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(invalidClassroom.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedClassroomValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldModifyClassroomAsync() { // given int randomNumber = GetRandomNumber(); int randomDays = randomNumber; DateTimeOffset randomDate = GetRandomDateTime(); DateTimeOffset randomInputDate = GetRandomDateTime(); Classroom randomClassroom = CreateRandomClassroom(randomInputDate); Classroom inputClassroom = randomClassroom; Classroom afterUpdateStorageClassroom = inputClassroom; Classroom expectedClassroom = afterUpdateStorageClassroom; Classroom beforeUpdateStorageClassroom = randomClassroom.DeepClone(); inputClassroom.UpdatedDate = randomDate; Guid classroomId = inputClassroom.Id; this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(classroomId)) .ReturnsAsync(beforeUpdateStorageClassroom); this.storageBrokerMock.Setup(broker => broker.UpdateClassroomAsync(inputClassroom)) .ReturnsAsync(afterUpdateStorageClassroom); // when Classroom actualClassroom = await this.classroomService.ModifyClassroomAsync(inputClassroom); // then actualClassroom.Should().BeEquivalentTo(expectedClassroom); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(classroomId), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateClassroomAsync(inputClassroom), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }