public void ShouldThrowServiceExceptionOnRetrieveAllTeacherAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();
            var expectedTeacherAttachmentServiceException = new TeacherAttachmentServiceException(exception);

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

            // when . then
            Assert.Throws <TeacherAttachmentServiceException>(() =>
                                                              this.teacherAttachmentService.RetrieveAllTeacherAttachments());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment();
            TeacherAttachment inputTeacherAttachment  = randomTeacherAttachment;
            var exception = new Exception();
            var expectedTeacherAttachmentServiceException = new TeacherAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(inputTeacherAttachment))
            .ThrowsAsync(exception);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentServiceException>(() =>
                                                                         addTeacherAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someTeacherId    = Guid.NewGuid();
            var  exception        = new Exception();
            var  expectedTeacherAttachmentException = new TeacherAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <TeacherAttachment> retrieveTeacherAttachmentTask =
                this.teacherAttachmentService.RetrieveTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentServiceException>(() =>
                                                                         retrieveTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
示例#4
0
        private TeacherAttachmentServiceException CreateAndLogServiceException(Exception exception)
        {
            var teacherAttachmentServiceException = new TeacherAttachmentServiceException(exception);

            this.loggingBroker.LogError(teacherAttachmentServiceException);

            return(teacherAttachmentServiceException);
        }