public void TestAddPost_InvalidPost_Failure(string title, string authorName, string body) { var mockPostValidator = new MockPostValidator(); var postDataAccess = new PostDataAccess(new MockPostRepo(), mockPostValidator, new MockAuthorRepo(), new MockAuthorValidator()); var post = new Post(title, new Author(authorName, 0), body); mockPostValidator.StubValidPost(false); Assert.Throws <ArgumentException>(() => postDataAccess.AddPost(post)); mockPostValidator.AssertIsValidPostCalled(); }
public void TestAddPost_ValidData_Success() { var mockPostRepo = new MockPostRepo(); var mockPostValidator = new MockPostValidator(); var mockAuthorValidator = new MockAuthorValidator(); var postDataAccess = new PostDataAccess(mockPostRepo, mockPostValidator, new MockAuthorRepo(), mockAuthorValidator); var author = new Author("author", 0); var post = new Post("title", author, "body"); mockPostValidator.StubValidPost(true); mockPostRepo.StubTryAddPostResult(post) .StubTryAddPostBool(true); mockAuthorValidator.StubIsValidAuthor(true); var returnedPost = postDataAccess.AddPost(post); mockPostValidator.AssertIsValidPostCalled(); mockPostRepo.AssertTryAddPostCalled(); mockAuthorValidator.AssertIsValidAuthorCalled(); Assert.NotNull(returnedPost); AssertPostsEqual(post, returnedPost); }
public void PostDataAccessAdd_True() { Post post = new Post() { id = 1, date = DateTime.Now, imgURL = "", numberOfComments = 1, postContent = "test content 1", postPreviewContent = "preview 1", title = "Post 1" }; var baseDataAccessMock = new Mock <IBaseDataAccess>(); PostDataAccess postDataAccess = new PostDataAccess(baseDataAccessMock.Object); baseDataAccessMock.Setup(m => m.Add(It.Is <SqlCommand>(c => c.CommandType.Equals(CommandType.StoredProcedure) && c.Parameters["@date"].Value.Equals(post.date) && c.Parameters["@title"].Value.Equals(post.title) && c.Parameters["@postPreviewContent"].Value.Equals(post.postPreviewContent) && c.Parameters["@postContent"].Value.Equals(post.postContent) && c.Parameters["@imgURL"].Value.Equals(post.imgURL) && c.Parameters["@numberOfComments"].Value.Equals(post.numberOfComments) ))).Returns(1); int id = postDataAccess.AddPost(post); baseDataAccessMock.Verify((m => m.Add(It.Is <SqlCommand>(c => c.CommandType.Equals(CommandType.StoredProcedure) && c.Parameters["@date"].Value.Equals(post.date) && c.Parameters["@title"].Value.Equals(post.title) && c.Parameters["@postPreviewContent"].Value.Equals(post.postPreviewContent) && c.Parameters["@postContent"].Value.Equals(post.postContent) && c.Parameters["@imgURL"].Value.Equals(post.imgURL) && c.Parameters["@numberOfComments"].Value.Equals(post.numberOfComments) ))), Times.Once()); Assert.Equal(1, id); }
public ActionResult PublishPost(int communityID, string title, string content, string postUserID, int hasPicture, string picPath) { if (postUserID == "") { return(Json("nouser")); } else { title = SensitiveWordFilter.CheckValidity(title); content = SensitiveWordFilter.CheckValidity(content); Post post = new Post(communityID, title, content, postUserID, hasPicture, picPath); PostDataAccess pda = new PostDataAccess(); bool addsuccess = pda.AddPost(post); if (addsuccess) { new LogDataAccess().AddLog(postUserID, new UserDataAccess().GetUserByID(postUserID).NickName, "发表了帖子,标题:" + title); return(Json("success")); } else { return(Json("error")); } } }