Ejemplo n.º 1
0
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedStudentExamServiceException =
                new StudentExamServiceException(exception);

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

            // when . then
            Assert.Throws <StudentExamServiceException>(() =>
                                                        this.studentExamService.RetrieveAllStudentExams());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentExamId = Guid.NewGuid();
            Guid inputStudentExamId  = randomStudentExamId;
            var  exception           = new Exception();

            var expectedStudentExamServiceException = new StudentExamServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(inputStudentExamId))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentExam> deleteStudentExamTask =
                this.studentExamService.DeleteStudentExamByIdAsync(inputStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   deleteStudentExamTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        private StudentExamServiceException CreateAndLogServiceException(Exception exception)
        {
            var studentExamServiceException = new StudentExamServiceException(exception);

            this.loggingBroker.LogError(studentExamServiceException);

            return(studentExamServiceException);
        }
Ejemplo n.º 4
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExam    randomStudentExam    = CreateRandomStudentExam(randomDateTime);
            StudentExam    someStudentExam      = randomStudentExam;

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

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(someStudentExam.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(someStudentExam);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   modifyStudentExamTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            StudentExam    randomStudentExam    = CreateRandomStudentExam(dateTime);
            StudentExam    inputStudentGuardian = randomStudentExam;

            inputStudentGuardian.UpdatedBy = inputStudentGuardian.CreatedBy;
            var serviceException = new Exception();

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentExamAsync(inputStudentGuardian))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentExam> addStudentGuardianTask =
                this.studentExamService.AddStudentExamAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   addStudentGuardianTask.AsTask());

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

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

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

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

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(someStudentExamId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentExam> retrieveStudentExamByIdTask =
                this.studentExamService.RetrieveStudentExamByIdAsync(someStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   retrieveStudentExamByIdTask.AsTask());

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

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

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

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