Ejemplo n.º 1
0
        public void ThrowInvalidOperationException_WhenUserIsNull()
        {
            // Arrange
            var contextMock        = new Mock <ITravelGuideContext>();
            var factoryMock        = new Mock <IArticleFactory>();
            var commentFactoryMock = new Mock <IArticleCommentFactory>();

            contextMock.Setup(x => x.Users.Find(It.IsAny <Guid>())).Returns((User)null);

            var service = new ArticleService(contextMock.Object, factoryMock.Object, commentFactoryMock.Object);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => service.AddComment("id", "content", Guid.NewGuid()));
        }
Ejemplo n.º 2
0
        //public ActionResult Comments(int? art)
        //{
        //    if (!art.HasValue || art.Value <= 0) return View();  //return Redirect("/Error/E404/");

        //    return View();
        //}

        //发布评论
        public ActionResult PublishCom(int artid, int comid, string contents)
        {
            //后期加上,用户是否已经登录

            string error    = "";
            var    comModel = service.AddComment(new ArticleComment()
            {
                ArticleId = artid,
                UserId    = 0,
                ParentId  = comid,
                Contents  = contents,
            }, out error);

            return(Json(new { state = comModel == null ? -1 : 1, error = error, comment = comModel }, JsonRequestBehavior.DenyGet));
        }
Ejemplo n.º 3
0
        public void CallSaveChanges_WhenInputIsValid()
        {
            // Arrange
            var contextMock        = new Mock <ITravelGuideContext>();
            var factoryMock        = new Mock <IArticleFactory>();
            var commentFactoryMock = new Mock <IArticleCommentFactory>();
            var article            = new Article();
            var comment            = new ArticleComment();
            var user = new User();

            contextMock.Setup(x => x.Users.Find(It.IsAny <string>())).Returns(user);
            commentFactoryMock.Setup(x => x.CreateArticleComment(It.IsAny <string>(), It.IsAny <User>(), It.IsAny <string>(), It.IsAny <Guid>())).Returns(comment);
            contextMock.Setup(x => x.Articles.Find(It.IsAny <Guid>())).Returns(article);

            var service = new ArticleService(contextMock.Object, factoryMock.Object, commentFactoryMock.Object);

            // Act
            service.AddComment("id", "content", Guid.NewGuid());

            // Assert
            contextMock.Verify(x => x.SaveChanges(), Times.Once);
        }
Ejemplo n.º 4
0
        public void AddCommentToArticleInstance_WhenInputIsValid()
        {
            // Arrange
            var contextMock        = new Mock <ITravelGuideContext>();
            var factoryMock        = new Mock <IArticleFactory>();
            var commentFactoryMock = new Mock <IArticleCommentFactory>();
            var article            = new Article();
            var comment            = new ArticleComment();
            var user = new User();

            contextMock.Setup(x => x.Users.Find(It.IsAny <string>())).Returns(user);
            commentFactoryMock.Setup(x => x.CreateArticleComment(It.IsAny <string>(), It.IsAny <User>(), It.IsAny <string>(), It.IsAny <Guid>())).Returns(comment);
            contextMock.Setup(x => x.Articles.Find(It.IsAny <Guid>())).Returns(article);

            var service = new ArticleService(contextMock.Object, factoryMock.Object, commentFactoryMock.Object);

            // Act
            service.AddComment("id", "content", Guid.NewGuid());

            // Assert
            Assert.AreSame(article.Comments.First(), comment);
        }