public void ShouldRedirectEditToIndexIfInvallidModelOnPostback()
        {
            // Arrange
            Person person = new Person { FirstName = "TestFirstName1", LastName = "TestLastName1" };

            var repositoryMock = new Mock<IRepository>();

            PersonController controller = new PersonController(repositoryMock.Object);
            controller.SetFakeControllerContext(); // Use the extension method in our MvcMoqHelper class

            // Add a validation broken rule so that the ModelState becomes invalid
            controller.ModelState.AddModelError("testkey", @"test error message");

            // Act
            ActionResult result = controller.Edit(person);

            // Assert
            repositoryMock.Verify(r => r.Update(person), Times.Never());
            repositoryMock.Verify(r => r.List<Person>(), Times.Never());

            // check that the controller returns a redirect to action where the action method is "Index"
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(RedirectToRouteResult), result.GetType());
            Assert.AreEqual("Index", (((RedirectToRouteResult)result)).RouteValues["action"]);
        }
        public void ShouldEditRedirectToIndexOnExceptionDuringPostback()
        {
            // Arrange
            Person person = new Person { FirstName = "TestFirstName1", LastName = "TestLastName1" };

            var repositoryMock = new Mock<IRepository>();

            PersonController controller = new PersonController(repositoryMock.Object);
            controller.SetFakeControllerContext(); // Use the extension method in our MvcMoqHelper class

            // Force the repository mock repository's Update method to throw an exception
            repositoryMock.Setup(r => r.Update(person)).Throws<InvalidOperationException>();

            // Act
            ActionResult result = controller.Edit(person);

            // Assert
            repositoryMock.Verify(r => r.Update(person), Times.AtLeastOnce());
            repositoryMock.Verify(r => r.SaveChanges(), Times.Never());
            repositoryMock.Verify(r => r.List<Person>(), Times.Never());

            // check that the controller returns a redirect to action where the action method is "Index"
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(RedirectToRouteResult), result.GetType());
            Assert.AreEqual("Index", (((RedirectToRouteResult)result)).RouteValues["action"]);
        }
        public void ShouldDisplayEditIfInvallidModelOnAjaxPostback()
        {
            // Arrange
            Person person = new Person { FirstName = "TestFirstName1", LastName = "TestLastName1" };

            var repositoryMock = new Mock<IRepository>();
            var httpRequestMock = new Mock<HttpRequestBase>();
            var contextMock = new Mock<HttpContextBase>();

            httpRequestMock.SetupGet(r => r.Headers).Returns(new System.Net.WebHeaderCollection
                                                                 {
                                                                     {"X-Requested-With", "XMLHttpRequest"}
                                                                 }
                );
            contextMock.SetupGet(x => x.Request).Returns(httpRequestMock.Object);

            PersonController controller = new PersonController(repositoryMock.Object);
            controller.ControllerContext = new ControllerContext(contextMock.Object, new RouteData(), controller);

            // Add a validation broken rule so that the ModelState becomes invalid
            controller.ModelState.AddModelError("testkey", @"test error message");

            // Act
            ActionResult result = controller.Edit(person);

            // Assert
            repositoryMock.Verify(r => r.Update(person), Times.Never());
            repositoryMock.Verify(r => r.List<Person>(), Times.Never());

            // check that the controller returns a redirect to action where the action method is "Index"
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(Person), ((ViewResultBase)(result)).Model.GetType());
            Assert.AreEqual(person.Id, ((Person)((ViewResultBase)(result)).Model).Id);

            Assert.IsNull(((ViewResultBase)result).ViewData["GenderList"]);
        }
        public void ShouldEditOnAjaxPostback()
        {
            // Note: to mock the controller's Request property to represent an Ajax request, see
            // http://stackoverflow.com/questions/970198/how-to-mock-the-request-on-controller-in-asp-net-mvc

            // Arrange
            Person person = new Person { FirstName = "TestFirstName1", LastName = "TestLastName1" };

            var repositoryMock = new Mock<IRepository>();
            var httpRequestMock = new Mock<HttpRequestBase>();
            var contextMock = new Mock<HttpContextBase>();

            httpRequestMock.SetupGet(r => r.Headers).Returns(new System.Net.WebHeaderCollection
                                                                 {
                                                                     {"X-Requested-With", "XMLHttpRequest"}
                                                                 }
                );
            contextMock.SetupGet(x => x.Request).Returns(httpRequestMock.Object);

            List<Person> persons = new List<Person>
                                       {
                                           new Person {Id = 1, FirstName = "TestFirstName1", LastName = "TestLastName1"},
                                           new Person {Id = 2, FirstName = "TestFirstName2", LastName = "TestLastName2"},
                                           new Person {Id = 3, FirstName = "TestFirstName3", LastName = "TestLastName3"}
                                       };
            repositoryMock.Setup(r => r.List<Person>()).Returns(persons);

            PersonController controller = new PersonController(repositoryMock.Object);
            controller.ControllerContext = new ControllerContext(contextMock.Object, new RouteData(), controller);

            // Act
            ActionResult result = controller.Edit(person);

            // Assert
            repositoryMock.Verify(r => r.Update(person), Times.Once());
            repositoryMock.Verify(r => r.SaveChanges(), Times.AtLeastOnce());
            repositoryMock.Verify(r => r.List<Person>(), Times.AtLeastOnce());

            Assert.IsNotNull(result);

            // check that the control returns a "_List" partial view with the results of the repository List<Person> method
            Assert.AreEqual(typeof(PartialViewResult), result.GetType());
            Assert.AreEqual("_List", (((PartialViewResult)result)).ViewName);
            //Assert.AreEqual(persons, (((PartialViewResult)result)).Model);
            Assert.AreEqual(persons.Count, ((List<Person>)(((PartialViewResult)result)).Model).Count);
            Assert.AreEqual(persons[1], ((List<Person>)(((PartialViewResult)result)).Model)[1]);

            Assert.IsNotNull(((PartialViewResult)result).ViewData["GenderList"]);
        }
        public void ShouldDisplayEdit()
        {
            // Arrange
            const int id = 1;
            Person person = new Person { Id = 1, FirstName = "TestFirstName1", LastName = "TestLastName1" };

            var repositoryMock = new Mock<IRepository>();
            PersonController controller = new PersonController(repositoryMock.Object);

            repositoryMock.Setup(r => r.GetById<Person>(id)).Returns(person);

            // Act
            ActionResult result = controller.Edit(id);

            // Assert
            repositoryMock.Verify(r => r.GetById<Person>(id), Times.Exactly(1));

            Assert.IsNotNull(result);

            // check the Model
            Assert.IsNotNull(result);

            // check that the control returns a "_Edit" partial view with the results of the repository GetById<Person> method
            Assert.AreEqual(typeof(PartialViewResult), result.GetType());
            Assert.AreEqual("_Edit", (((PartialViewResult)result)).ViewName);
            Assert.AreEqual(typeof(Person), ((PartialViewResult)(result)).Model.GetType());
            Assert.AreEqual(person.Id, ((Person)((PartialViewResult)(result)).Model).Id);
        }