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 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 ShouldCreateOnPostback()
        {
            // 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

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

            // Assert
            repositoryMock.Verify(r => r.Insert(person), Times.Once());
            repositoryMock.Verify(r => r.SaveChanges(), Times.AtLeastOnce());

            // 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"]);
        }