public void Can_Edit_Tour() { // Arrange - create the mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); mock.Setup(m => m.Tours).Returns(new Tour[] { new Tour {TourID = 1, Name = "P1"}, new Tour {TourID = 2, Name = "P2"}, new Tour {TourID = 3, Name = "P3"}, }); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act Tour p1 = target.Edit(1).ViewData.Model as Tour; Tour p2 = target.Edit(2).ViewData.Model as Tour; Tour p3 = target.Edit(3).ViewData.Model as Tour; // Assert Assert.AreEqual(1, p1.TourID); Assert.AreEqual(2, p2.TourID); Assert.AreEqual(3, p3.TourID); }
public void Cannot_Edit_Nonexistent_Tour() { // Arrange - create the mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); mock.Setup(m => m.Tours).Returns(new Tour[] { new Tour {TourID = 1, Name = "P1"}, new Tour {TourID = 2, Name = "P2"}, new Tour {TourID = 3, Name = "P3"}, }); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act Tour result = (Tour)target.Edit(4).ViewData.Model; // Assert Assert.IsNull(result); }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a tour Tour tour = new Tour { Name = "Test" }; // Act - try to save the tour ActionResult result = target.Edit(tour); // Assert - check that the repository was called mock.Verify(m => m.SaveTour(tour)); // Assert - check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a tour Tour tour = new Tour { Name = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the tour ActionResult result = target.Edit(tour); // Assert - check that the repository was not called mock.Verify(m => m.SaveTour(It.IsAny<Tour>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }