Inheritance: DataModel
コード例 #1
0
 public ActionResult Create(Post post, int authorId)
 {
     if (ModelState.IsValid) {
         post.Author = repository.Find<Account>(x => x.Id == authorId);
         repository.Save(post);
         return RedirectToAction("Index");
     }
     return RedirectToAction("New");
 }
コード例 #2
0
        public void UpdateForSave_ShouldSetModifiedTime()
        {
            var dataModel = new Post();

            var theDate = DateTime.Now;
            dataModel.UpdateForSave(theDate);

            Assert.That(dataModel.ModifiedDate, Is.EqualTo(theDate));
        }
コード例 #3
0
        public void UpdateForSave_ShouldSetCreatedTime_WhenIdNotSet()
        {
            var dataModel = new Post();

            var theDate = DateTime.Now;
            dataModel.UpdateForSave(theDate);

            Assert.That(dataModel.CreatedDate, Is.EqualTo(theDate));
        }
コード例 #4
0
        public void Create_ShouldValidateThePost()
        {
            const int authorId = 1;
            var post = new Post();
            controller.SetupContext(post);

            controller.Create(post, authorId);

            Assert.IsFalse(controller.ModelState.IsValid);
        }
コード例 #5
0
        public void Edit_ShouldFindAndSetTheAuthor()
        {
            const int authorId = 1;
            var post = new Post();
            repository.Stub(p => p.Find<Account>(x => true)).IgnoreArguments().Return(new Account());
            repository.Stub(p => p.Save(post)).Return(post);

            var result = controller.Update(post, authorId);

            Assert.IsTrue(controller.ModelState.IsValid);
            Assert.That(result.View(), Is.EqualTo("Show"));
        }
コード例 #6
0
        public ActionResult Update(Post post, int authorId)
        {
            if (ModelState.IsValid) {
                post.Author = new Account { Id = authorId };
                repository.Save(post);
                return RedirectToAction("Show", post.Id);
            }

            ViewBag.Authors = repository.FindAll<Account>();
            var refreshedPost = repository.Find<Post>(x => x.Id == post.Id);

            return View("Edit", new { post = refreshedPost });
        }
コード例 #7
0
        public void Edit_ShouldIncludeAuthorsInViewBagAndSetPost()
        {
            const int postId = 1;
            var post = new Post();
            var allAccounts = new List<Account>();
            repository.Stub(p => p.FindAll<Account>()).Return(allAccounts);
            repository.Stub(p => p.Find<Post>(x => true)).IgnoreArguments().Return(post);

            var result = (ViewResult)controller.Edit(postId);

            Assert.IsTrue(result.ViewBag.Authors.Equals(allAccounts));
            Assert.IsTrue(result.Model.Equals(post));
        }