public void CreateBookPostShouldNotSaveWhenBookPostIsInvalid()
        {
            var repository = new Mock<Repository>();
            var book = new Book { Id = 1 };
            var bookPost = new BookPost { Title = "The Book Post", Content = "some content", Type = BookPost.BookPostType.Reviews };
            var submittedBookPostInformation = new BookPostInformation(book.Id, bookPost);
            repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
            var bookPostsController = new BookPostsController(repository.Object);
            bookPostsController.ModelState.AddModelError("test error", "test exception");

            var result = (ViewResult)bookPostsController.Create(submittedBookPostInformation);

            repository.Verify(repo => repo.Edit(book), Times.Never(), "failing model validation should prevent creating book post");
            Assert.AreEqual("There were problems saving this book post", bookPostsController.TempData["flashError"]);
        }
        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 ShouldDeleteBookPostAndShowListOfBooksPosts()
        {
            var mockedRepo = new Mock<Repository>();
            var book = new Book {Id = 1};
            var bookPost = new BookPost {Id = 2, Title = "something"};
            book.Posts.Add(bookPost);
            mockedRepo.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
            var bookPostsController = new BookPostsController(mockedRepo.Object);

            var viewResult = bookPostsController.Delete(bookPost.Id, book.Id);
            mockedRepo.Verify(repo => repo.Edit(book));
            Assert.AreEqual("Book Post successfully deleted", bookPostsController.TempData["flashSuccess"]);
            Assert.AreEqual("Details", viewResult.RouteValues["action"]);
            Assert.AreEqual(book.Id, viewResult.RouteValues["id"]);
        }
 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 ShouldStoreBookPostWhenCreated()
 {
     var repository = new Mock<Repository>();
     var book = new Book { Id = 1 };
     var bookPost = new BookPost {Title = "test title", Content = "some content"};
     var submittedBookPostInformation = new BookPostInformation(book.Id, bookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var controller = new BookPostsController(repository.Object);
     var result = (RedirectToRouteResult)controller.Create(submittedBookPostInformation);
     repository.Verify(it => it.Edit(book), Times.Once());
     Assert.AreEqual(string.Format("Added {0} successfully", bookPost.Title), controller.TempData["flashSuccess"]);
     Assert.AreEqual(book.Id, result.RouteValues["id"]);
 }
        public void ShouldRedirectToDetailsPageWhenBookPostIsCreated()
        {
            var repository = new Mock<Repository>();
            var book = new Book { Id = 1 };
            var bookPost = new BookPost { Title = "The Book Post", Content = "some content", Type = BookPost.BookPostType.Reviews };
            var submittedBookPostInformation = new BookPostInformation(book.Id, bookPost);
            repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
            var bookPostsController = new BookPostsController(repository.Object);
            var viewResult = (RedirectToRouteResult)bookPostsController.Create(submittedBookPostInformation);

            repository.Verify(repo => repo.Edit(book));

            Assert.IsNotNull(viewResult);
            Assert.AreEqual("Added The Book Post successfully", bookPostsController.TempData["flashSuccess"]);
            Assert.AreEqual(1, viewResult.RouteValues["id"]);
            Assert.IsTrue(book.Posts.Contains(bookPost));
        }
 public void ShouldSetCreatedAtAndUpdatedAtOnBookPostCreation()
 {
     var now = DateTime.Now;
     var repository = new Mock<Repository>();
     var book = new Book { Id = 1 };
     var bookPost = new BookPost { Title = "test title", Content = "some content" };
     var submittedBookPostInformation = new BookPostInformation(book.Id, bookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var controller = new BookPostsController(repository.Object);
     controller.Create(submittedBookPostInformation);
     Assert.IsTrue(bookPost.CreatedAt >= now);
     Assert.IsTrue(bookPost.UpdatedAt >= now);
 }
        public void ShouldNotStoreBookPostWhenTypeIsInvalid()
        {
            var repository = new Mock<Repository>();
            var submittedBookPost = new BookPost { Title = "test title", Content = "derp" };
            repository.Setup(repo => repo.Create(submittedBookPost)).Returns(submittedBookPost);

            var controller = new BookPostsController(repository.Object);
            ValidateViewModel(submittedBookPost, controller);
            Assert.IsFalse(controller.ModelState.IsValid);
        }
 public void ShouldKnowToRenderTheBookPostContentAsMarkdown()
 {
     var id = 12;
     var repository = new Mock<Repository>();
     var book = new Book { Id = 1 };
     var markdown = new Markdown();
     var savedBookPost = new BookPost { Id = id, Title = "test title", Content = "Hello\n=====\nWorld", Type = BookPost.BookPostType.Reviews };
     book.Posts.Add(savedBookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var controller = new BookPostsController(repository.Object);
     var result = controller.Details(id, book.Id);
     repository.Verify(it => it.Get<Book>(book.Id), Times.Once());
     var transformedContent = markdown.Transform(savedBookPost.Content);
     Assert.AreEqual(transformedContent, result.ViewBag.transformedContent);
     Assert.AreEqual(id, ((BookPostInformation)result.Model).Model.Id);
 }
Пример #10
0
 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());
 }
Пример #11
0
        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());
        }
Пример #12
0
 public void ShouldKnowHowToDisplayABookPost()
 {
     var id = 12;
     var repository = new Mock<Repository>();
     var book = new Book { Id = 1 };
     var savedBookPost = new BookPost { Id = id, Title = "test title", Content = "some content", Type = BookPost.BookPostType.Reviews};
     book.Posts.Add(savedBookPost);
     repository.Setup(repo => repo.Get<Book>(book.Id)).Returns(book);
     var controller = new BookPostsController(repository.Object);
     var result = controller.Details(id, book.Id);
     repository.Verify(it => it.Get<Book>(book.Id), Times.Once());
     Assert.AreEqual(book.Id, ((BookPostInformation)result.Model).BookId);
     Assert.AreEqual(id, ((BookPostInformation)result.Model).Model.Id);
     Assert.AreEqual(book, ((BookPostInformation)result.Model).Book);
 }