public void Cannot_Edit_Nonexistent_Location() { // Arrange - create the mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); mock.Setup(m => m.Locations).Returns(new List<Location> { new Location {Id = 1, Name = "L1", State="MI" }, new Location {Id = 2, Name = "L2", State="IL"}, new Location {Id = 3, Name = "L3", State="MI"} }); // Arrange LocationsController target = new LocationsController(mock.Object); //Action Location result = (Location)target.Edit(4).ViewData.Model; //Assert Assert.IsNull(result); }
public void Cannot_Save_Invalid_Changes_To_Location() { //Arrange - create mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); //Arrange - create the controller LocationsController target = new LocationsController(mock.Object); //Arrange - create a location Location loc = new Location { Name = "Test" }; //Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); //Act - try to save the location ActionResult result = target.Edit(loc); //Assert - check that the repo was called mock.Verify(m => m.SaveLocation(It.IsAny<Location>()), Times.Never()); //Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Save_Valid_Changes_To_Location() { //Arrange - create mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); //Arrange - create the controller LocationsController target = new LocationsController(mock.Object); //Arrange - create a location Location loc = new Location { Id= 1, Name = "Test", Address1 = "123 N. Address", City = "Troy", Zip = "87654",State="AZ", }; //Act - try to save the location ActionResult result = target.Edit(loc); //Assert - check that the repo was called mock.Verify(m => m.SaveLocation(loc)); //Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Edit_Location() { // Arrange - create the mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); mock.Setup(m => m.Locations).Returns(new List<Location> { new Location {Id = 1, Name = "L1", State="MI" }, new Location {Id = 2, Name = "L2", State="IL"}, new Location {Id = 3, Name = "L3", State="MI"}, new Location {Id = 4, Name = "L4", State="IN"}, new Location {Id = 5, Name = "L5", State="MI"} }); // Arrange LocationsController target = new LocationsController(mock.Object); //Action Location l1 = target.Edit(1).ViewData.Model as Location; Location l2 = target.Edit(2).ViewData.Model as Location; Location l3 = target.Edit(3).ViewData.Model as Location; //Assert Assert.AreEqual(1, l1.Id); Assert.AreEqual(2, l2.Id); Assert.AreEqual(3, l3.Id); }