public void EditDBExceptionRepopulatesAuthorsDropdown()
        {
            //Arrange
            var repository       = Mock.Create <IRepository>();
            int bookId           = 1;
            int authorId         = 1;
            var exceptionMessage = "test exception with message";
            var book             = new BookEditViewModel()
            {
                AuthorId = AuthorHelper.RobertMartin(authorId).AuthorId,
                BookId   = BookHelper.CleanCode(bookId, authorId).BookId,
                Title    = BookHelper.CleanCode(bookId, authorId).Title,
                Genre    = BookHelper.CleanCode(bookId, authorId).Genre,
            };

            Mock.Arrange(() => repository.Save((Book)Arg.AnyObject)).Throws(new Exception(exceptionMessage)).OccursOnce();
            var authorsList = new List <Author>()
            {
                AuthorHelper.RobertMartin(1), AuthorHelper.JRRTolkien(authorId)
            };

            Mock.Arrange(() => repository.GetAllAuthors()).Returns(authorsList).OccursOnce();

            //Act
            var controller = new HomeController(repository);

            controller.ControllerContext = Mock.Create <ControllerContext>(); //needed by TryValidateModel(entity)
            var result = controller.Edit(book) as ViewResult;


            //Assert
            StandardAssertsForEditErrors(controller, result, book, exceptionMessage);
            Mock.Assert(repository);
        }
        public void EditViewModelValidationErrorRepopulatesAuthorsDropdown()
        {
            //Arrange
            var    repository   = Mock.Create <IRepository>();
            int    bookId       = 1;
            int    authorId     = 3;
            string errorMessage = "The minimal length for the title is 3";
            var    book         = new BookEditViewModel()
            {
                AuthorId = AuthorHelper.RobertMartin(authorId).AuthorId,
                BookId   = BookHelper.CleanCode(bookId, authorId).BookId,
                Title    = "12", //Too short, min is 3 (defined in the view model)
                Genre    = BookHelper.CleanCode(bookId, authorId).Genre,
            };

            Mock.Arrange(() => repository.Save((Book)Arg.AnyObject)).OccursNever();
            var authorsList = new List <Author>()
            {
                AuthorHelper.RobertMartin(1), AuthorHelper.JRRTolkien(authorId)
            };

            Mock.Arrange(() => repository.GetAllAuthors()).Returns(authorsList).OccursOnce();

            //Act
            var controller = new HomeController(repository);

            controller.ModelState.AddModelError("Title", errorMessage);       //add the view model validation error
            controller.ControllerContext = Mock.Create <ControllerContext>(); //needed by TryValidateModel(entity)
            var result = controller.Edit(book) as ViewResult;

            //Assert
            StandardAssertsForEditErrors(controller, result, book, errorMessage);
            Mock.Assert(repository);
        }
        public void EditIndexShowsBookEditFormWithBookContentWithPopulatedDropDownList()
        {
            //Arrange
            var repository = Mock.Create <IRepository>();
            var bookToEdit = BookHelper.CleanCode(bookId: 2, authorId: 1);

            Mock.Arrange(() => repository.GetBook(bookToEdit.BookId)).Returns(bookToEdit).OccursOnce();
            //called to populate the dropdownlist
            Mock.Arrange(() => repository.GetAllAuthors()).Returns(
                new List <Author>()
            {
                AuthorHelper.RobertMartin(1),
                AuthorHelper.RoyOsherove(2),
            }).OccursOnce();

            //Act
            var controller   = new HomeController(repository);
            var result       = controller.Edit(bookToEdit.BookId) as ViewResult;
            var model        = result.Model as BookEditViewModel;
            var selectedItem = model.Authors.Find(b => Int32.Parse(b.Value) == bookToEdit.AuthorId);

            //Assert
            Assert.IsNotNull(model);
            Assert.AreEqual(model.BookId, bookToEdit.BookId);
            Assert.AreEqual(model.AuthorId, bookToEdit.AuthorId);
            Assert.AreEqual(model.Genre, bookToEdit.Genre);
            Assert.AreEqual(model.Title, bookToEdit.Title);
            Assert.IsTrue(selectedItem.Selected);
            Assert.IsNull(result.ViewBag.Message);
            Mock.Assert(repository);
        }
        public void CreateWithDBExceptionAddsErrorToModelStateAndRepopulatesAuthorsDropdown()
        {
            //Arrange
            var repository       = Mock.Create <IRepository>();
            int bookId           = 1;
            int authorId         = 1;
            var exceptionMessage = "Unable to save changes. Try again, and if the problem persists, see your system administrator.";
            var book             = new BookEditViewModel()
            {
                AuthorId = AuthorHelper.RobertMartin(authorId).AuthorId,
                BookId   = BookHelper.CleanCode(bookId, authorId).BookId,
                Title    = BookHelper.CleanCode(bookId, authorId).Title,
                Genre    = BookHelper.CleanCode(bookId, authorId).Genre,
            };

            Mock.Arrange(() => repository.CreateBook((Book)Arg.AnyObject)).Throws(new Exception()).OccursOnce();
            var authorsList = new List <Author>()
            {
                AuthorHelper.RobertMartin(1), AuthorHelper.JRRTolkien(authorId)
            };

            Mock.Arrange(() => repository.GetAllAuthors()).Returns(authorsList).OccursOnce();

            //Act
            var controller = new HomeController(repository);

            controller.ControllerContext = Mock.Create <ControllerContext>(); //needed by TryValidateModel(entity)
            var result         = controller.Create(book) as ViewResult;
            var model          = result.Model as BookEditViewModel;
            var errorStates    = from m in controller.ModelState.Values select m.Errors;
            var selectedAuthor = model.Authors.Find(a => a.Selected);

            //Assert
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(exceptionMessage, errorStates.First().First().ErrorMessage);
            Assert.AreEqual(book.AuthorId.ToString(), selectedAuthor.Value);
            Assert.IsNull(controller.ViewBag.Message);
            Assert.IsNull(controller.TempData["Message"]);
            Mock.Assert(repository);
        }
        public void CreateIndexShowsEmptyCreationFormWithPopulatedDropDownList()
        {
            //Arrange
            var repository  = Mock.Create <IRepository>();
            var authorsList = new List <Author>()
            {
                AuthorHelper.RobertMartin(1), AuthorHelper.JRRTolkien(2)
            };

            Mock.Arrange(() => repository.GetAllAuthors()).Returns(authorsList).OccursOnce();

            //Act
            var        controller = new HomeController(repository);
            ViewResult result     = controller.Create();
            var        model      = result.Model as BookEditViewModel;

            //Assert
            Assert.IsInstanceOfType(model, typeof(BookEditViewModel));
            Assert.AreEqual(2, model.Authors.Count);
            Assert.IsFalse(model.Authors.Exists(a => a.Selected));
            Assert.IsNull(result.ViewBag.Message);
            Mock.Assert(repository);
        }
        public void FindByGenreReturnsAllInGenre()
        {
            //Arrange
            var repository = Mock.Create <IRepository>();

            Mock.Arrange(() => repository.GetBooksByGenre(BookHelper.ArtOfUnitTesting(1).Genre)).Returns(new List <Book>()
            {
                BookHelper.ArtOfUnitTesting(1),
                BookHelper.CleanCode(2)
            }).MustBeCalled();

            //Act
            var        controller = new HomeController(repository);
            ViewResult result     = controller.FindByGenre("Programming");
            var        model      = result.Model as IEnumerable <Book>;

            //Assert
            Assert.AreEqual(2, model.Count());
            Assert.AreEqual(AuthorHelper.RoyOsherove().FullName, model.ToList()[0].Author.FullName);
            Assert.AreEqual(AuthorHelper.RobertMartin().FullName, model.ToList()[1].Author.FullName);
            Assert.AreEqual(BookHelper.ArtOfUnitTesting().Title, model.ToList()[0].Title);
            Assert.AreEqual(BookHelper.CleanCode().Title, model.ToList()[1].Title);
            Mock.Assert(repository);
        }