public void TestHomeControllerAddCommentWithBadCommentObj()
 {
     var comment = new comment();
     var ex = new Exception();
     db.Setup(c => c.comments.Add(comment)).Throws(ex);
     PartialViewResult result = controller.AddComment(comment) as PartialViewResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("_ErrorPartial", result.ViewName);
     Assert.IsInstanceOfType(result.Model, typeof(HttpStatusCodeResult));
     var model = result.Model as HttpStatusCodeResult;
     Assert.AreEqual(400, model.StatusCode);
     Assert.AreEqual("Comment could not be added.", model.StatusDescription);
 }
 public void TestHomeControllerAddCommentOnWorkout()
 {
     comment comment = new comment();
     comment.message = "Test comment";
     comment.date_created = DateTime.Now;
     comment.User_id = 1;
     comment.Workout_id = 1;
     db.Setup(c => c.comments.Add(comment)).Returns(comment);
     RedirectToRouteResult result = controller.AddComment(comment) as RedirectToRouteResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("Index", result.RouteValues["action"], "action was not Index");
     Assert.AreEqual("Home", result.RouteValues["controller"], "controller was not FavoriteWorkouts");
 }
 public void TestHomeControllerAddCommentWithNoUserError()
 {
     controller = new CommentsController(db.Object)
     {
         ControllerContext = MockContext.AuthenticationContext("not_a_real_user")
     };
     var comment = new comment();
     PartialViewResult result = controller.AddComment(comment) as PartialViewResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("_ErrorPartial", result.ViewName);
     Assert.IsInstanceOfType(result.Model, typeof(HttpStatusCodeResult));
     var model = result.Model as HttpStatusCodeResult;
     Assert.AreEqual(400, model.StatusCode);
     Assert.AreEqual("Comment could not be added.", model.StatusDescription);
 }
Exemplo n.º 4
0
        public ActionResult AddComment(comment comment)
        {
            if (comment == null)
            {
                return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "No comment to add was specified."));
            }

            comment.date_created = DateTime.Now;
            comment.User_id = userAccess.getUserId(User.Identity.Name);
            if (Session["workout_id"] != null) comment.Workout_id = (int)Session["workout_id"];
            if (ModelState.IsValid)
            {
                try
                {
                    db.comments.Add(comment);
                    db.SaveChanges();
                    if (this.Request.UrlReferrer != null)
                    {
                        string url = this.Request.UrlReferrer.PathAndQuery;
                        return Redirect(url);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                catch
                {
                    return PartialView("_ErrorPartial", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Comment could not be added."));
                }
            }
            else
            {
                return PartialView("_ErrorPartial", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid comment."));
            }
        }
 public void TestHomeControllerAddCommentWithValidationErrs()
 {
     var comment = new comment();
     controller.ModelState.AddModelError("Fail", "Failed");
     PartialViewResult result = controller.AddComment(comment) as PartialViewResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("_ErrorPartial", result.ViewName);
     Assert.IsInstanceOfType(result.Model, typeof(HttpStatusCodeResult));
     var model = result.Model as HttpStatusCodeResult;
     Assert.AreEqual(400, model.StatusCode);
     Assert.AreEqual("Invalid comment.", model.StatusDescription);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the fake comments
        /// </summary>
        /// <returns>A list of fake comments</returns>
        private List<comment> getSeedComments()
        {
            List<workout> workouts = getSeedWorkouts();
            List<user> users = getSeedUsers();

            comment comment1 = new comment
            {
                id = 1,
                message = "Comment1",
                date_created = Convert.ToDateTime("2015-01-01"),
                user = users[1],
                workout = workouts[1]
            };
            comment comment2 = new comment
            {
                id = 2,
                message = "Comment2",
                date_created = Convert.ToDateTime("2015-02-02"),
                user = users[1],
                workout = workouts[2]
            };
            comment comment3 = new comment
            {
                id = 3,
                message = "Comment3",
                date_created = Convert.ToDateTime("2015-03-03"),
                user = users[2],
                workout = workouts[1],
            };
            var comment = new List<comment> { comment1, comment2, comment3 };
            return comment;
        }
 public void TestAdminCommentsPostDeleteWithDbUpdateException()
 {
     comment t = new comment()
     {
         id = 1
     };
     db.Setup(c => c.comments.Find(t.id)).Returns(t);
     db.Setup(c => c.comments.Remove(t)).Returns(t);
     db.Setup(c => c.SaveChanges()).Throws(new DbUpdateException());
     ViewResult result = adminCon.DeleteConfirmed(t) as ViewResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("DetailedError", result.ViewName);
     Assert.IsInstanceOfType(result.Model, typeof(HttpStatusCodeResult));
     var model = result.Model as HttpStatusCodeResult;
     Assert.AreEqual(500, model.StatusCode);
     Assert.AreEqual("Failed to delete the comment as it may be referenced in the database.", model.StatusDescription);
 }
 public void TestAdminCommentsPostDeleteCommentNotFound()
 {
     comment t = new comment();
     ViewResult result = adminCon.DeleteConfirmed(t) as ViewResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("DetailedError", result.ViewName);
     Assert.IsInstanceOfType(result.Model, typeof(HttpStatusCodeResult));
     var model = result.Model as HttpStatusCodeResult;
     Assert.AreEqual(500, model.StatusCode);
     Assert.AreEqual("The comment does not exist or has already been deleted", model.StatusDescription);
 }