public ActionResult Save( EditMode editMode, string id, string title, string body, PostListOrder postListOrder) { if (String.IsNullOrEmpty(title)) { ModelState.AddModelError("title", "Title must not be empty."); } var postCategory = GetPostCategory(editMode, id); postCategory.Title = title; postCategory.Body = body; postCategory.PostListOrder = postListOrder; if (!ModelState.IsValid) { var editData = new EditPostCategoryData { EditMode = editMode, PostCategory = postCategory }; return(View("Edit", editData)); } if (editMode == EditMode.Add) { _postCategoryRepository.Save(postCategory); } _postCategoryRepository.SubmitChanges(); return(RedirectToAction("Detail", new { id = postCategory.ID })); }
public void SaveExistingTest( [Values(PostListOrder.Date, PostListOrder.Title)] PostListOrder postListOrder) { var editMode = EditMode.Edit; var postCategoryID = "TestPostCategoryID"; var title = "A post category title"; var body = string.Empty; var postCategory = new PostCategory(); postCategory.ID = postCategoryID; postCategory.Title = "oldTitle"; postCategory.Body = "oldBody"; postCategory.PostListOrder = (PostListOrder)Int32.MaxValue; _postCategoryRepositoryMock.Expect(r => r.Get(postCategoryID)).Return(postCategory); _postCategoryRepositoryMock.Expect( r => r.SubmitChanges()) .WhenCalled( b => VerifyPostCategory(postCategory, postCategoryID, title, body, postListOrder)); var result = TestedController.Save(editMode, postCategoryID, title, body, postListOrder); VerifyRedirectToRouteResult(result, expectedAction: "Detail", expectedID: postCategoryID); }
public void SaveNewTest( [Values(PostListOrder.Date, PostListOrder.Title)] PostListOrder postListOrder) { var editMode = EditMode.Add; var postCategoryID = Guid.Empty.ToString(); var title = "A post category title"; var body = string.Empty; var postCategory = new PostCategory(); postCategory.ID = postCategoryID; _postCategoryRepositoryMock.Expect(r => r.Create()).Return(postCategory); _postCategoryRepositoryMock.Expect(r => r.Save(postCategory)); _postCategoryRepositoryMock.Expect( r => r.SubmitChanges()) .WhenCalled( b => VerifyPostCategory(postCategory, postCategoryID, title, body, postListOrder)); var result = TestedController.Save(editMode, postCategoryID, title, body, postListOrder); VerifyRedirectToRouteResult(result, expectedAction: "Detail", expectedID: postCategoryID); }
private static void VerifyPostCategory(PostCategory postCategoryToVerify, string expectedPostCategoryID, string expectedTitle, string expectedBody, PostListOrder expectedPostListOrder) { Assert.AreEqual(postCategoryToVerify.ID, expectedPostCategoryID, "PostCategory ID has been modified."); Assert.AreEqual(postCategoryToVerify.Title, expectedTitle, "Title not set."); Assert.AreEqual(postCategoryToVerify.Body, expectedBody, "Body not set."); Assert.AreEqual(postCategoryToVerify.PostListOrder, expectedPostListOrder, "PostListOrder not set."); }