コード例 #1
0
        public async Task ShouldThrowValidationExceptionOnModifyWhenCommentBodyIsInvalidAndLogItAsync(
            string invalidCommentBody)
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Comment        randomComment  = CreateRandomComment(dateTime);
            Comment        invalidComment = randomComment;

            invalidComment.Body = invalidCommentBody;

            var invalidCommentException = new InvalidCommentException(
                parameterName: nameof(Comment.Body),
                parameterValue: invalidComment.Body);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentException);

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(int minutes)
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Comment        randomComment = CreateRandomComment(dateTime);
            Comment        inputComment  = randomComment;

            inputComment.CreatedDate = dateTime.AddMinutes(minutes);
            inputComment.UpdatedDate = inputComment.CreatedDate;

            var invalidCommentValidationException = new InvalidCommentException(
                parameterName: nameof(Comment.CreatedDate),
                parameterValue: inputComment.CreatedDate);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentValidationException);

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

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

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

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

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


            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #3
0
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomCommentId = default;
            Guid inputCommentId  = randomCommentId;

            var invalidCommentInputException = new InvalidCommentException(
                parameterName: nameof(Comment.Id),
                parameterValue: inputCommentId);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentInputException);

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

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() => deleteCommentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #4
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCommentIdIsInvalidAndLogItAsync()
        {
            //given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Comment        randomComment = CreateRandomComment(dateTime);
            Comment        inputComment  = randomComment;

            inputComment.Id = default;

            var invalidCommentInputException = new InvalidCommentException(
                parameterName: nameof(Comment.Id),
                parameterValue: inputComment.Id);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentInputException);

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

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  registerCommentTask.AsTask());

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

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

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