Esempio n. 1
0
        public async Task CommentTest_ReturnsDetailsView_WhenCommentExists()
        {
            // Arrange
            var postId = 1;
            var res    = false;
            IEnumerable <Comments> objectsList = new List <Comments>
            {
                new Comments {
                    CommentMsg = "mock article 1", PostID = 1, CommentedDate = DateTime.Now
                },
                new Comments {
                    CommentMsg = "mock article 2", PostID = 2, CommentedDate = DateTime.Now
                }
            };

            blogPostServMock
            .Setup(repo => repo.GetAllComments(postId))
            .Returns(Task.FromResult(objectsList));

            // Act
            var result = await blogController.BlogComments(postId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <Comments> >(viewResult.ViewData.Model);

            Assert.Equal(2, model.Count());
            if (model.Count() == 2)
            {
                res = true;
            }
            File.AppendAllText("../../../../output_revised.txt",
                               "CommentTest_ReturnsDetailsView_WhenCommentExists="
                               + res.ToString() + "\n");
        }
        public async Task CommentTest_ReturnsNotFound_WhenCommentDoesNotExist()
        {
            // Arrange
            var res    = false;
            var postId = 12;

            blogPostServMock.Setup(repo => repo.GetAllComments(postId)).
            Returns(Task.FromResult <IEnumerable <Comments> >(null));

            // Act
            var result = await blogController.BlogComments(postId);

            // Assert
            var viewResult = Assert.IsType <NotFoundResult>(result);

            if (viewResult != null)
            {
                res = true;
            }
            File.AppendAllText("../../../../output_exception_revised.txt",
                               "CommentTest_ReturnsNotFound_WhenCommentDoesNotExist="
                               + res.ToString() + "\n");
        }