예제 #1
0
        public void CreateShouldCreateNewTweetProperly()
        {
            var newTweet = new CreateTweetModel()
            {
                Content = "This is text."
            };

            Assert.AreEqual(4, this.helper.Tweets.Count);

            var viewResult = this.controller.Create(newTweet) as RedirectToRouteResult;

            Assert.IsNotNull(viewResult, "View is Null!");

            Assert.AreEqual(5, this.helper.Tweets.Count);
        }
예제 #2
0
        public ActionResult Create(CreateTweetModel model)
        {
            if (ModelState.IsValid)
            {
                var hashTags = this.Data.HashTags.All().ToList();
                var tweet    = new Tweet();
                var author   = this.Data.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);
                tweet.Content  = model.Content;
                tweet.Date     = DateTime.Now;
                tweet.Author   = author;
                tweet.HashTags = base.GetTagsFromContent(model.Content, hashTags);
                author.Tweets.Add(tweet);
                this.Data.Tweets.Add(tweet);
                this.Data.SaveChanges();

                return(RedirectToAction("Profile", "Users", new { username = User.Identity.Name }));
            }

            return(PartialView("_Create", model));
        }
예제 #3
0
        public ActionResult Create(CreateTweetModel tweet)
        {
            if (ModelState.IsValid)
            {
                var user = this.db.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name);

                var newTweet = new Tweet
                {
                    Message  = tweet.Message,
                    AuthorId = user.Id
                };

                db.Tweets.Add(newTweet);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //ViewBag.AuthorId = new SelectList(db.Users, "Id", "Email", tweet.AuthorId);
            return(View(tweet));
        }
예제 #4
0
        public void CreateShouldNotAddTweetInCaseOfInvalidModel()
        {
            // Max content length allowed is 140
            var newTweet = new CreateTweetModel()
            {
                Content = "Error."
            };

            Assert.AreEqual(4, this.helper.Tweets.Count);

            this.controller.ViewData.ModelState.AddModelError("key", "ErrorMessage");
            var viewResult = this.controller.Create(newTweet) as PartialViewResult;

            Assert.IsNotNull(viewResult, "View is Null!");

            Assert.AreEqual(4, this.helper.Tweets.Count);

            var model = viewResult.Model as CreateTweetModel;

            Assert.IsNotNull(model, "Model is Null!");
            Assert.AreEqual(model, newTweet);
        }