public void EditBookShouldNotSaveWhenBookIsInvalid()
        {
            var bookPost = new BookPost();
            var book = new Book { Id = 1 };
            book.Posts.Add(bookPost);
            var mockedRepo = new Mock<Repository>();
            var bookPostsController = new BookPostsController(mockedRepo.Object);
            mockedRepo.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
            bookPostsController.ModelState.AddModelError("test error", "test exception");

            var result = (ViewResult)bookPostsController.Edit(new BookPostInformation(book.Id, bookPost));

            mockedRepo.Verify(repo => repo.Edit(book), Times.Never(), "failing model validation should prevent updating book post");
            Assert.AreEqual("There were problems saving this book post", bookPostsController.TempData["flashError"]);
        }
 public void ShouldKnowHowToUpdateABookPost()
 {
     var repository = new Mock<Repository>();
     var book = new Book {Id = 1};
     var existingBookPost = new BookPost { Id = 1, Title = "Derping for dummies", Type = BookPost.BookPostType.Reviews };
     book.Posts.Add(existingBookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var bookPostsController = new BookPostsController(repository.Object);
     var result = (RedirectToRouteResult)bookPostsController.Edit(new BookPostInformation(book.Id, existingBookPost));
     Assert.AreEqual(existingBookPost.Id, result.RouteValues["id"]);
     Assert.AreEqual("Updated Derping for dummies successfully", bookPostsController.TempData["flashSuccess"]);
     repository.Verify(repo => repo.Edit(book), Times.Once());
 }
 public void ShouldSetCreatedAtFromExisitingBookPost()
 {
     var repository = new Mock<Repository>();
     var book = new Book { Id = 1 };
     var existingBookPost = new BookPost { Id = 1, Title = "Derping for dummies", Type = BookPost.BookPostType.Reviews, CreatedAt = DateTime.Now.AddMinutes(-1)};
     book.Posts.Add(existingBookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var bookPostsController = new BookPostsController(repository.Object);
     var editedBookPost = new BookPost { Id = 1, Title = "Derping for dummies II", Type = BookPost.BookPostType.Reviews, CreatedAt = DateTime.Now };
     bookPostsController.Edit(new BookPostInformation(book.Id, editedBookPost));
     Assert.AreEqual(existingBookPost.CreatedAt, editedBookPost.CreatedAt);
 }
        public void ShouldKnowHowToRenderAnEditPage()
        {
            var repositoryMock = new Mock<Repository>();
            var bookPost = new BookPost { Id = 1, Title = "The Page", Type = BookPost.BookPostType.Reviews };
            var book = new Book {Id = 1};
            book.Posts.Add(bookPost);
            repositoryMock.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
            var bookPostsController = new BookPostsController(repositoryMock.Object);

            var result = bookPostsController.Edit(bookPost.Id, book.Id);
            var actualModel = (BookPostInformation)result.Model;

            Assert.AreEqual(bookPost.Title, actualModel.Model.Title);
            Assert.AreEqual("PUT", bookPostsController.ViewBag.Method);
            repositoryMock.Verify(repo => repo.Get<Book>(book.Id), Times.Once());
        }