public void ShouldThrowServiceExceptionOnRetrieveAllStudentExamFeesWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedStudentExamFeeServiceException =
                new StudentExamFeeServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllStudentExamFees())
            .Throws(exception);

            // when . then
            Assert.Throws <StudentExamFeeServiceException>(() =>
                                                           this.studentExamFeeService.RetrieveAllStudentExamFees());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAllStudentExamFees(),
                                          Times.Once);

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #2
0
        private StudentExamFeeServiceException CreateAndLogServiceException(Exception exception)
        {
            var studentExamFeeServiceException = new StudentExamFeeServiceException(exception);

            this.loggingBroker.LogError(studentExamFeeServiceException);

            return(studentExamFeeServiceException);
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExamFee someStudentExamFee   = CreateRandomStudentExamFee(randomDateTime);

            someStudentExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedStudentExamFeeServiceException =
                new FailedStudentExamFeeServiceException(serviceException);

            var expectedStudentExamFeeServiceException =
                new StudentExamFeeServiceException(failedStudentExamFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamFeeByIdsAsync(
                                             It.IsAny <Guid>(),
                                             It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDateTime);

            // when
            ValueTask <StudentExamFee> modifyStudentExamFeeTask =
                this.studentExamFeeService.ModifyStudentExamFeeAsync(someStudentExamFee);

            // then
            await Assert.ThrowsAsync <StudentExamFeeServiceException>(() =>
                                                                      modifyStudentExamFeeTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentExamFeeByIdsAsync(
                                              It.IsAny <Guid>(),
                                              It.IsAny <Guid>()),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            StudentExamFee someStudentExamFee = CreateRandomStudentExamFee(dateTime);

            someStudentExamFee.UpdatedBy   = someStudentExamFee.CreatedBy;
            someStudentExamFee.UpdatedDate = someStudentExamFee.CreatedDate;
            var serviceException = new Exception();

            var failedStudentExamFeeServiceException =
                new FailedStudentExamFeeServiceException(serviceException);

            var expectedStudentExamFeeServiceException =
                new StudentExamFeeServiceException(failedStudentExamFeeServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentExamFeeAsync(It.IsAny <StudentExamFee>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentExamFee> createStudentExamFeeTask =
                this.studentExamFeeService.AddStudentExamFeeAsync(someStudentExamFee);

            // then
            await Assert.ThrowsAsync <StudentExamFeeServiceException>(() =>
                                                                      createStudentExamFeeTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentExamFeeAsync(It.IsAny <StudentExamFee>()),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId    = Guid.NewGuid();
            Guid someExamFeeId    = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedStudentExamFeeServiceException =
                new FailedStudentExamFeeServiceException(serviceException);

            var expectedStudentExamFeeServiceException =
                new StudentExamFeeServiceException(failedStudentExamFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamFeeByIdsAsync(
                                             It.IsAny <Guid>(), It.IsAny <Guid>()))
            .Throws(serviceException);

            // when
            ValueTask <StudentExamFee> removeStudentExamFeeTask =
                this.studentExamFeeService.RemoveStudentExamFeeByIdAsync(someStudentId,
                                                                         someExamFeeId);

            // then
            await Assert.ThrowsAsync <StudentExamFeeServiceException>(() =>
                                                                      removeStudentExamFeeTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentExamFeeByIdsAsync(
                                              It.IsAny <Guid>(), It.IsAny <Guid>()),
                                          Times.Once);

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteStudentExamFeeAsync(It.IsAny <StudentExamFee>()),
                                          Times.Never);

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