public void ShouldDisplayCreate() { // Arrange var repositoryMock = new Mock<IRepository>(); Person person = new Person(); PersonController controller = new PersonController(repositoryMock.Object); // Act ActionResult result = controller.Create(); // Assert Assert.IsNotNull(result); // check the Model Assert.IsNotNull(result); Assert.AreEqual(typeof(Person), ((ViewResultBase)(result)).Model.GetType()); Assert.AreEqual(person.Id, ((Person)((ViewResultBase)(result)).Model).Id); }
public void ShouldCreateOnAjaxPostback() { // 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.Create(person); // Assert repositoryMock.Verify(r => r.Insert(person), Times.Once()); repositoryMock.Verify(r => r.SaveChanges(), 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]); }
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"]); }