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 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);
        }