public void ShouldAddNewPerson() { _repository.BeginTransaction(); IEnumerable<Person> persons = _repository.List<Person>(); int priorCount = persons.Count(); Person newPerson = new Person { FirstName = "testFirstName", LastName = "testLastName", DateOfBirth = DateTime.Today.AddYears(-32), Gender = "M" }; // Add the new person to the context Person person = _repository.Insert(newPerson); // Save the context to the database, causing the database to be updated _repository.SaveChanges(); // At this stage, the database has the new row, so the identity has been incremented and assigned Assert.IsTrue(person.Id > 0); Assert.IsTrue(newPerson.Id > 0); persons = _repository.List<Person>(); int postCount = persons.Count(); Assert.AreEqual(postCount, priorCount + 1); // Rollback the transaction, to make the test repeatable _repository.RollbackTransaction(); }
public void ShouldAddNewPerson() { using (new TransactionScope()) { using (TrailsContext context = new TrailsContext()) { DbSet<Person> persons = context.Persons; persons.Load(); int priorCount = persons.Local.Count; Person newPerson = new Person { FirstName = "testFirstName", LastName = "testLastName", DateOfBirth = DateTime.Today.AddYears(-32), Gender = "M" }; // Add the new person to the context Person person = persons.Add(newPerson); // Save the context to the database, causing the database to be updated context.SaveChanges(); // At this stage, the database has the new row, so the identity has been incremented and assigned Assert.IsTrue(person.Id > 0); Assert.IsTrue(newPerson.Id > 0); persons.Load(); int postCount = persons.Local.Count; Assert.AreEqual(postCount, priorCount + 1); } // Rollback the transaction, to make the test repeatable } }
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 ActionResult Create(Person person) { ViewData["GenderList"] = PopulateGenderList(); if (ModelState.IsValid) { _repository.Insert(person); _repository.SaveChanges(); } else { if (Request.IsAjaxRequest()) return View("Create", person); else return RedirectToAction("Index"); } if (Request.IsAjaxRequest()) { IList<Person> persons = new List<Person>(_repository.List<Person>()); return PartialView("_List", persons); } else return RedirectToAction("Index"); }
// // GET: /Person/Create public ActionResult Create() { Person person = new Person(); return PartialView("_Create", person); }
public ActionResult Edit(Person person) { try { IList<Person> persons = null; if (ModelState.IsValid) { _repository.Update(person); _repository.SaveChanges(); } else { if (Request.IsAjaxRequest()) return View("Edit", person); else return RedirectToAction("Index"); } ViewData["GenderList"] = PopulateGenderList(); persons = new List<Person>(_repository.List<Person>()); if (Request.IsAjaxRequest()) return PartialView("_List", persons); else return RedirectToAction("Index"); } catch { return RedirectToAction("Index"); } }
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 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); }
public void ShouldDisplayDetails() { // Arrange const int id = 1; Person person = new Person {Id = 1, FirstName = "TestFirstName1", LastName = "TestLastName1"}; var repositoryMock = new Mock<IRepository>(); repositoryMock.Setup(r => r.GetById<Person>(id)).Returns(person).Verifiable("GetById was not called with the correct Id"); // Note: using Verifiable() is not considered best practice - see http://code.google.com/p/moq/issues/detail?id=220 // use .Verify() instead PersonController controller = new PersonController(repositoryMock.Object); // Act ViewResult result = controller.Details(id); // Assert repositoryMock.Verify(); repositoryMock.Verify(r => r.GetById<Person>(id), Times.Exactly(1)); Assert.IsNotNull(result); // check the view data Assert.IsNotNull(result.ViewData["GenderList"]); Assert.IsTrue(result.ViewData["GenderList"].GetType() == typeof(Dictionary<string, string>)); Assert.IsTrue(((Dictionary<string, string>)result.ViewData["GenderList"]).ContainsKey("M")); // check the Model Assert.IsNotNull(result.Model); Assert.AreEqual(person, result.Model); }
public void ShouldDisplayDelete() { // 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.Delete(id); // Assert repositoryMock.Verify(r => r.GetById<Person>(id), Times.Exactly(1)); 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 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 ShouldDeleteOnPostback() { // 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.DeleteConfirmed(id); // Assert repositoryMock.Verify(r => r.GetById<Person>(id), Times.Once()); repositoryMock.Verify(r => r.Delete(person), Times.Once()); repositoryMock.Verify(r => r.SaveChanges(), Times.Once()); // Assert Assert.IsNotNull(result); // check that the controller returns a redirect to action where the action method is "Index" 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"]); }