Esempio n. 1
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            CourseAttachment someCourseAttachment = CreateRandomCourseAttachment();
            var exception = new Exception();

            var expectedCourseAttachmentServiceException =
                new CourseAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()))
            .ThrowsAsync(exception);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentServiceException>(() =>
                                                                        addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public void ShouldThrowServiceExceptionOnRetrieveAllCourseAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedCourseAttachmentServiceException =
                new CourseAttachmentServiceException(exception);

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

            // when . then
            Assert.Throws <CourseAttachmentServiceException>(() =>
                                                             this.courseAttachmentService.RetrieveAllCourseAttachments());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private CourseAttachmentServiceException CreateAndLogServiceException(Exception exception)
        {
            var CourseAttachmentServiceException = new CourseAttachmentServiceException(exception);

            this.loggingBroker.LogError(CourseAttachmentServiceException);

            return(CourseAttachmentServiceException);
        }
Esempio n. 4
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someCourseId     = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedCourseAttachmentServiceException =
                new FailedCourseAttachmentServiceException(serviceException);

            var expectedCourseAttachmenServiceException =
                new CourseAttachmentServiceException(failedCourseAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CourseAttachment> removeCourseAttachmentTask =
                this.courseAttachmentService.RemoveCourseAttachmentByIdAsync(
                    someCourseId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <CourseAttachmentServiceException>(() =>
                                                                        removeCourseAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId),
                                          Times.Once);

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

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

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