public void _Call_UserService_GetUserById(string text, int postId, string userId)
        {
            //Arrange
            var mockedPostService      = new Mock <IPostService>();
            var mockedNewsService      = new Mock <INewsService>();
            var mockedUserService      = new Mock <IUserService>();
            var mockedCommentFactory   = new Mock <ICommentFactory>();
            var mockedRepository       = new Mock <IRepository <Comment> >();
            var mockedUnitOfWork       = new Mock <IUnitOfWork>();
            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var commentService = new CommentService(
                mockedPostService.Object,
                mockedNewsService.Object,
                mockedCommentFactory.Object,
                mockedUserService.Object,
                mockedRepository.Object,
                mockedDateTimeProvider.Object,
                mockedUnitOfWork.Object);


            //Act
            commentService.AddCommentToPost(text, postId, userId);

            //Assert
            mockedUserService.Verify(d => d.GetUserById(userId), Times.Once);
        }
Пример #2
0
        public void AddCommentToPost_ShouldCallRepositoryAddAndUnitOFWorkComplete()
        {
            // Arrange
            var post = new Post {
                Id = Guid.NewGuid()
            };
            var comment = new Comment
            {
                PostId   = post.Id,
                AuthorId = "userid",
                Content  = "content",
            };

            var repositoryMock     = new Mock <IRepository <Comment> >();
            var postRepositoryMock = new Mock <IRepository <Post> >();

            postRepositoryMock.Setup(x => x.QueryAll).Returns(new[] { post }.AsQueryable());
            var unitOfWorkMock = new Mock <IUnitOfWork>();

            var postService = new CommentService(unitOfWorkMock.Object, repositoryMock.Object, postRepositoryMock.Object);

            // Act
            postService.AddCommentToPost((Guid)comment.PostId, comment.AuthorId, comment.Content);

            // Assert
            repositoryMock.Verify(x => x.Add(It.Is <Comment>(c =>
                                                             c.PostId == comment.PostId &&
                                                             c.AuthorId == comment.AuthorId &&
                                                             c.Content == comment.Content)), Times.Once);
            unitOfWorkMock.Verify(x => x.Complete(), Times.Once);
            Assert.AreEqual(post.CommentCount, 1);
        }
Пример #3
0
        public void AddCommentToPost_ShouldDoNothingIfPostDoesntExist()
        {
            // Arrange
            var repositoryMock     = new Mock <IRepository <Comment> >();
            var postRepositoryMock = new Mock <IRepository <Post> >();
            var unitOfWorkMock     = new Mock <IUnitOfWork>();

            var postService = new CommentService(unitOfWorkMock.Object, repositoryMock.Object, postRepositoryMock.Object);

            // Act
            postService.AddCommentToPost(Guid.NewGuid(), "id", "content");

            // Assert
            unitOfWorkMock.Verify(x => x.Complete(), Times.Never);
        }
        public void _Call_NewsService_AddComment(string text, int postId, string userId)
        {
            //Arrange
            var user    = new User();
            var date    = new DateTime();
            var comment = new Comment();

            var mockedPostService = new Mock <IPostService>();
            var mockedNewsService = new Mock <INewsService>();

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(u => u.GetUserById(It.IsAny <string>())).Returns(user);

            var mockedCommentFactory = new Mock <ICommentFactory>();

            mockedCommentFactory.Setup(c => c.CreateComment(It.IsAny <User>(), It.IsAny <DateTime>(), It.IsAny <string>()))
            .Returns(comment);

            var mockedRepository = new Mock <IRepository <Comment> >();
            var mockedUnitOfWork = new Mock <IUnitOfWork>();

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            mockedDateTimeProvider.Setup(d => d.GetCurrentDate()).Returns(date);

            var commentService = new CommentService(
                mockedPostService.Object,
                mockedNewsService.Object,
                mockedCommentFactory.Object,
                mockedUserService.Object,
                mockedRepository.Object,
                mockedDateTimeProvider.Object,
                mockedUnitOfWork.Object);


            //Act
            commentService.AddCommentToPost(text, postId, userId);

            //Assert
            mockedPostService.Verify(d => d.AddComment(postId, comment), Times.Once);
        }
Пример #5
0
 public string AddCommentInPost(string title, string content, int postId, string authorId)
 {
     return(commentService.AddCommentToPost(title, content, postId, authorId));
 }