public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedDateNotSameAsCreateDateAndLogItAsync() { // given int randomNumber = GetRandomNumber(); int randomMinutes = randomNumber; DateTimeOffset randomDate = GetRandomDateTime(); StudentExam randomStudentExam = CreateRandomStudentExam(randomDate); StudentExam invalidStudentExam = randomStudentExam; invalidStudentExam.UpdatedDate = randomDate; StudentExam storageStudentExam = randomStudentExam.DeepClone(); Guid studentId = invalidStudentExam.StudentId; Guid semesterCourseId = invalidStudentExam.ExamId; invalidStudentExam.CreatedDate = storageStudentExam.CreatedDate.AddMinutes(randomNumber); var invalidStudentExamException = new InvalidStudentExamException( parameterName: nameof(StudentExam.CreatedDate), parameterValue: invalidStudentExam.CreatedDate); var expectedStudentExamValidationException = new StudentExamValidationException(invalidStudentExamException); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamByIdAsync(invalidStudentExam.Id)) .ReturnsAsync(storageStudentExam); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <StudentExam> modifyStudentExamTask = this.studentExamService.ModifyStudentExamAsync(invalidStudentExam); // then await Assert.ThrowsAsync <StudentExamValidationException>(() => modifyStudentExamTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamByIdAsync(invalidStudentExam.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldModifyStudentExamAsync() { // given int randomNumber = GetRandomNumber(); int randomDays = randomNumber; DateTimeOffset randomDate = GetRandomDateTime(); DateTimeOffset randomInputDate = GetRandomDateTime(); StudentExam randomStudentExam = CreateRandomStudentExam(randomInputDate); StudentExam inputStudentExam = randomStudentExam; StudentExam afterUpdateStorageStudentExam = inputStudentExam; StudentExam expectedStudentExam = afterUpdateStorageStudentExam; StudentExam beforeUpdateStorageStudentExam = randomStudentExam.DeepClone(); inputStudentExam.UpdatedDate = randomDate; Guid studentId = inputStudentExam.StudentId; Guid guardianId = inputStudentExam.ExamId; this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamByIdAsync(inputStudentExam.Id)) .ReturnsAsync(beforeUpdateStorageStudentExam); this.storageBrokerMock.Setup(broker => broker.UpdateStudentExamAsync(inputStudentExam)) .ReturnsAsync(afterUpdateStorageStudentExam); // when StudentExam actualStudentExam = await this.studentExamService.ModifyStudentExamAsync(inputStudentExam); // then actualStudentExam.Should().BeEquivalentTo(expectedStudentExam); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamByIdAsync(inputStudentExam.Id), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateStudentExamAsync(inputStudentExam), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }