Example #1
0
        public void TestEditPost_ValidData_Success()
        {
            var mockPostRepo        = new MockPostRepo();
            var mockPostValidator   = new MockPostValidator();
            var mockAuthorValidator = new MockAuthorValidator();
            var postDataAccess      = new PostDataAccess(mockPostRepo, mockPostValidator, new MockAuthorRepo(), mockAuthorValidator);
            var post         = new Post("OriginalTitle", new Author("OriginalAuthor", 0), "OriginalBody");
            var expectedPost = new Post("newTitle", new Author("newAuthor", 0), "newBody", post.Timestamp, post.PostID);
            var listOfPosts  = new List <Post>()
            {
                post
            };

            mockPostValidator.StubPostExists(true)
            .StubValidPost(true);
            mockPostRepo.StubGetAllPosts(listOfPosts)
            .StubTryEditPostBool(true)
            .StubTryEditPostResult(expectedPost);
            mockAuthorValidator.StubIsValidAuthor(true);
            var returnedPost = postDataAccess.EditPost(post);

            mockPostValidator.AssertPostExistsCalled();
            mockPostRepo.AssertGetAllPostsCalled();
            mockPostValidator.AssertIsValidPostCalled();
            mockPostRepo.AssertTryEditPostCalled();
            mockAuthorValidator.AssertIsValidAuthorCalled();
            Assert.NotNull(returnedPost);
            AssertPostsEqual(expectedPost, returnedPost);
        }
Example #2
0
        public void TestAddPost_InvalidAuthor_Failure(string title, string authorName, string body)
        {
            var mockPostValidator   = new MockPostValidator();
            var mockAuthorValidator = new MockAuthorValidator();
            var postDataAccess      = new PostDataAccess(new MockPostRepo(), mockPostValidator, new MockAuthorRepo(), mockAuthorValidator);
            var author = new Author(authorName, 0);
            var post   = new Post(title, author, body);

            mockPostValidator.StubValidPost(true);
            mockAuthorValidator.StubIsValidAuthor(false);

            Assert.Throws <ArgumentException>(() => postDataAccess.AddPost(post));
            mockPostValidator.AssertIsValidPostCalled();
            mockAuthorValidator.AssertIsValidAuthorCalled();
        }
Example #3
0
        public void TestEditPost_PostExistsButInvalidAuthor()
        {
            var mockPostValidator   = new MockPostValidator();
            var mockAuthorValidator = new MockAuthorValidator();
            var postDataAccess      = new PostDataAccess(new MockPostRepo(), mockPostValidator, new MockAuthorRepo(), mockAuthorValidator);
            var post = new Post("", new Author("", 0), "");

            mockPostValidator.StubPostExists(true)
            .StubValidPost(true);
            mockAuthorValidator.StubIsValidAuthor(false);

            Assert.Throws <ArgumentException>(() => postDataAccess.EditPost(post));
            mockPostValidator.AssertPostExistsCalled();
            mockPostValidator.AssertIsValidPostCalled();
            mockAuthorValidator.AssertIsValidAuthorCalled();
        }
Example #4
0
        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);
        }