Exemplo n.º 1
0
        public async Task AddCommentTest_ReturnsCommentSuccessfullyAdded()
        {
            // arrange
            var mockArticleId = 1;
            var mockComment   = new Comment {
                Title = "mock comment", Content = "test"
            };

            _commentsRepositoryMock.Setup(repo => repo.InsertAsync(mockComment)).Returns(Task.CompletedTask);
            _commentsRepositoryMock.Setup(repo => repo.SaveChangesAsync()).Returns(Task.CompletedTask);

            // act
            var fetch = await _commentsController.Post(mockArticleId, mockComment.AsDto());

            // assert
            _commentsRepositoryMock.Verify(repo => repo.InsertAsync(It.IsAny <Comment>()), Times.Once);

            var result = Assert.IsType <OkObjectResult>(fetch);

            Assert.NotNull(result);
            var response = (ApiResponse)result.Value;

            Assert.NotNull(response);
            Assert.True(response.IsSuccess);
            Assert.Equal(mockComment.Title, ((CommentModel)response.Result).Title);
            Assert.Equal(mockComment.Content, ((CommentModel)response.Result).Content);
            Assert.Equal(mockArticleId, ((CommentModel)response.Result).ArticleId);
        }