Example #1
0
        public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCommentId = Guid.NewGuid();
            var  exception     = new Exception();

            var expectedCommentServiceException =
                new CommentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(exception);

            // when
            ValueTask <Comment> retrieveByIdCommentTask =
                this.commentService.RetrieveCommentByIdAsync(someCommentId);

            // then
            await Assert.ThrowsAsync <CommentServiceException>(() =>
                                                               retrieveByIdCommentTask.AsTask());

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

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

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

            var expectedStudentCommentException =
                new CommentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(inputCommentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <Comment> deleteStudentCommentTask =
                this.commentService.RemoveCommentByIdAsync(inputCommentId);

            // then
            await Assert.ThrowsAsync <CommentServiceException>(() =>
                                                               deleteStudentCommentTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllCommentsWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedCommentServiceException =
                new CommentServiceException(exception);

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

            // when . then
            Assert.Throws <CommentServiceException>(() =>
                                                    this.commentService.RetrieveAllComments());

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

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

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

            this.loggingBroker.LogError(commentServiceException);

            return(commentServiceException);
        }
Example #5
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Comment        randomComment = CreateRandomComment(dateTime);
            Comment        inputComment  = randomComment;
            var            exception     = new Exception();

            var expectedCommentServiceException =
                new CommentServiceException(exception);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCommentAsync(inputComment))
            .ThrowsAsync(exception);

            // when
            ValueTask <Comment> createCommentTask =
                this.commentService.AddCommentAsync(inputComment);

            // then
            await Assert.ThrowsAsync <CommentServiceException>(() =>
                                                               createCommentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Comment        randomComment        = CreateRandomComment(randomDateTime);
            Comment        someComment          = randomComment;

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

            var expectedCommentServiceException =
                new CommentServiceException(serviceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(someComment.Id))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Comment> modifyCommentTask =
                this.commentService.ModifyCommentAsync(someComment);

            // then
            await Assert.ThrowsAsync <CommentServiceException>(() =>
                                                               modifyCommentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCommentByIdAsync(someComment.Id),
                                          Times.Once);

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

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