public async Task ShouldThrowDependencyValidationExceptionOnRegisterIfBadRequestErrorOccursAndLogItAsync(
            Exception validationApiException)
        {
            // given
            Student someStudent = CreateRandomStudent();

            var expectedDepndencyValidationException =
                new StudentDependencyValidationException(
                    validationApiException);

            this.apiBrokerMock.Setup(broker =>
                                     broker.PostStudentAsync(It.IsAny <Student>()))
            .ThrowsAsync(validationApiException);

            // when
            ValueTask <Student> registerStudentTask =
                this.studentService.RegisterStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentDependencyValidationException>(() =>
                                                                            registerStudentTask.AsTask());

            this.apiBrokerMock.Verify(broker =>
                                      broker.PostStudentAsync(It.IsAny <Student>()),
                                      Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(
                                                              SameExceptionAs(expectedDepndencyValidationException))),
                                          Times.Once);

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        private StudentDependencyValidationException CreateAndLogDependencyValidationException(Exception exception)
        {
            var studentDependencyValidationException = new StudentDependencyValidationException(exception);

            this.loggingBroker.LogError(studentDependencyValidationException);

            return(studentDependencyValidationException);
        }
        public async Task ShouldThrowDependencyValidationExceptionOnRegisterWhenStudentAlreadyExistsAndLogItAsync()
        {
            // given
            Student someStudent = CreateRandomStudent();
            string  someMessage = GetRandomMessage();

            var duplicateKeyException =
                new DuplicateKeyException(someMessage);

            var alreadyExistsStudentException =
                new AlreadyExistsStudentException(duplicateKeyException);

            var expectedStudentDependencyValidationException =
                new StudentDependencyValidationException(alreadyExistsStudentException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(duplicateKeyException);

            // when
            ValueTask <Student> registerStudentTask =
                this.studentService.RegisterStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentDependencyValidationException>(() =>
                                                                            registerStudentTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameValidationExceptionAs(
                                                                    expectedStudentDependencyValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentAsync(someStudent),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }