public async Task ShouldThrowValidationExceptionOnModifyIfClassroomDoesntExistAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); DateTimeOffset dateTime = GetRandomDateTime(); Classroom randomClassroom = CreateRandomClassroom(dateTime); Classroom nonExistentClassroom = randomClassroom; nonExistentClassroom.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); Classroom noClassroom = null; var notFoundClassroomException = new NotFoundClassroomException(nonExistentClassroom.Id); var expectedClassroomValidationException = new ClassroomValidationException(notFoundClassroomException); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(nonExistentClassroom.Id)) .ReturnsAsync(noClassroom); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Classroom> modifyClassroomTask = this.classroomService.ModifyClassroomAsync(nonExistentClassroom); // then await Assert.ThrowsAsync <ClassroomValidationException>(() => modifyClassroomTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(nonExistentClassroom.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 ShouldThrowValidatonExceptionOnDeleteWhenStorageClassroomIsInvalidAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); Classroom randomClassroom = CreateRandomClassroom(dates: randomDateTime); Guid inputClassroomId = randomClassroom.Id; Classroom inputClassroom = randomClassroom; Classroom nullStorageClassroom = null; var notFoundClassroomException = new NotFoundClassroomException(inputClassroomId); var expectedClassroomValidationException = new ClassroomValidationException(notFoundClassroomException); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(inputClassroomId)) .ReturnsAsync(nullStorageClassroom); // when ValueTask <Classroom> actualClassroomTask = this.classroomService.RemoveClassroomAsync(inputClassroomId); // then await Assert.ThrowsAsync <ClassroomValidationException>(() => actualClassroomTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedClassroomValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(inputClassroomId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteClassroomAsync(It.IsAny <Classroom>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnRetrieveWhenStorageClassroomIsNullAndLogItAsync() { //given Guid randomClassroomId = Guid.NewGuid(); Guid inputClassroomId = randomClassroomId; Classroom invalidStorageClassroom = null; var notFoundClassroomException = new NotFoundClassroomException(inputClassroomId); var expectedClassroomValidationException = new ClassroomValidationException(notFoundClassroomException); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(inputClassroomId)) .ReturnsAsync(invalidStorageClassroom); //when ValueTask <Classroom> retrieveClassroomByIdTask = this.classroomService.RetrieveClassroomById(inputClassroomId); //then await Assert.ThrowsAsync <ClassroomValidationException>(() => retrieveClassroomByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedClassroomValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }