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"); }
public void UpdateForSave_ShouldSetModifiedTime() { var dataModel = new Post(); var theDate = DateTime.Now; dataModel.UpdateForSave(theDate); Assert.That(dataModel.ModifiedDate, Is.EqualTo(theDate)); }
public void UpdateForSave_ShouldSetCreatedTime_WhenIdNotSet() { var dataModel = new Post(); var theDate = DateTime.Now; dataModel.UpdateForSave(theDate); Assert.That(dataModel.CreatedDate, Is.EqualTo(theDate)); }
public void Create_ShouldValidateThePost() { const int authorId = 1; var post = new Post(); controller.SetupContext(post); controller.Create(post, authorId); Assert.IsFalse(controller.ModelState.IsValid); }
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")); }
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 }); }
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)); }