public void AddCommentToUnsavedPostThrowsInvalidOperationException() { var post = new Post { Title = "The Not Lonely Post" }; var repository = new PostRepository(_db); Assert.That(() => repository.AddComment(post, new Comment { Content = "Here I am!" }), Throws.InvalidOperationException); }
public void AddComment(Post post, Comment comment) { if (post.Id == null) { throw new InvalidOperationException("Post has no ID"); } if (post.Comments == null) { post.Comments = new List<Comment>(); } post.Comments.Add(comment); _collection.Save(post); }
public ActionResult Add(Post post) { // must validate the post if (!String.IsNullOrEmpty(post.Title) && !String.IsNullOrEmpty(post.Content)) { // save the post post.Posted = DateTime.Now; _postRepository.Insert(post); // list the post after save return RedirectToAction("List"); } return View(); }
public void AddCommentAddsComment() { var post = new Post { Title = "The Not Lonely Post" }; _db.GetCollection<Post>("posts").Insert(post); var repository = new PostRepository(_db); repository.AddComment(post, new Comment { Content = "Here I am!" }); var posts = (from p in _db.GetCollection<Post>("posts").AsQueryable<Post>() where p.Title == "The Not Lonely Post" select p).ToArray(); Assert.That(posts, Has.Exactly(1).Not.Null); Assert.That(posts, Has.Exactly(0).Null); var comments = posts[0].Comments; Assert.That(comments, Is.Not.Null); Assert.That(comments, Has.Exactly(1).Not.Null); Assert.That(comments, Has.Exactly(0).Null); Assert.That(comments[0].Content, Is.EqualTo("Here I am!")); }
public void Insert(Post post) { _collection.Insert(post); }