public void Can_Retrieve_Image_Data() { // Arrange - create a location with image data Location loc = new Location { Id = 2, Name = "Test" }; // Arrange - create the mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); mock.Setup(m => m.Locations).Returns(new Location[] { new Location {Id=1, Name = "P1" }, loc, new Location {Id=3, Name = "P3" } }.AsQueryable()); // Arrange - create the controller LocImageController target = new LocImageController(mock.Object); // Act - call the GetImage action method ActionResult result = target.GetImage(2); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(FileResult)); }
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 static string LocationToString(Location loc) { string locString = "<table class='table table-striped'><tr><th>ID:</th><td>" + loc.Id.ToString() + "</td></tr>"; locString += "<tr><th>Name:</th><td>" + loc.Name + "</td></tr>"; locString += "<tr><th>Subtitle:</th><td>" + loc.Subtitle + "</td></tr>"; locString += "<tr><th>Manager Name:</th><td>" + loc.ManagerName + "</td></tr>"; locString += "<tr><th>Address Line 1:</th><td>" + loc.Address1 + "</td></tr>"; locString += "<tr><th>Address Line 2:</th><td>" + loc.Address2 + "</td></tr>"; locString += "<tr><th>City:</th><td>" + loc.City + "</td></tr>"; locString += "<tr><th>State:</th><td>" + loc.State + "</td></tr>"; locString += "<tr><th>Zip:</th><td>" + loc.Zip + "</td></tr>"; locString += "<tr><th>AtmOnly:</th><td>" + loc.AtmOnly.ToString() + "</td></tr>"; locString += "<tr><th>NoAtm:</th><td>" + loc.NoAtm.ToString() + "</td></tr>"; locString += "<tr><th>Description:</th><td>" + loc.Description + "</td></tr>"; locString += "<tr><th>Latitude:</th><td>" + loc.Latitude + "</td></tr>"; locString += "<tr><th>Longitude:</th><td>" + loc.Longitude + "</td></tr>"; locString += "</table>"; return locString; }
public void Can_Delete_Valid_Locations() { //Arrange - create a location Location loc = new Location { Id = 2, Name = "Test", Address1 = "123 N. Address", City = "Troy", Zip = "87654" }; // Arrange - create the mock repository Mock<ILocationRepository> mock = new Mock<ILocationRepository>(); mock.Setup(m => m.Locations).Returns(new Location[] { new Location {Id = 1 , Name = "L1", Address1 = "123 N. Address", City = "Troy", Zip="87654" }, loc, new Location {Id = 3 , Name = "L3" , Address1 = "123 N. Address", City = "Troy", Zip="87654" } }); // Arrange - create the controller LocationsController target = new LocationsController(mock.Object); // Act - delete the product target.Delete(loc.Id); //Assert - ensure that the repository delete method was called with the correct location mock.Verify(m => m.DeleteLocation(loc.Id)); }
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 ActionResult Edit(Location loc) { /***** Logging initial settings *****/ DbChangeLog log = new DbChangeLog(); log.UserName = User.Identity.Name; log.Controller = "Locations"; log.Action = (loc.Id != 0) ? "Edit" : "Create"; if (log.Action == "Edit") { Location oldLoc = repo.Locations.FirstOrDefault(m => m.Id == loc.Id); log.BeforeChange = Domain.Extensions.DbLogExtensions.LocationToString(oldLoc); } /***** end Logging initial settings *****/ if (ModelState.IsValid) { try { repo.SaveLocation(loc); log.AfterChange = Domain.Extensions.DbLogExtensions.LocationToString(loc); log.Success = true; TempData["message"] = string.Format("{0} has been saved", loc.Name); } catch(Exception e) { log.Error = e.ToString(); log.Success = false; TempData["alert"] = string.Format("There has been an error. {0} has not been saved", loc.Name); } log.ItemId = loc.Id; } else { log.Error = "Errors: "; foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { log.Error += error + "<br />"; } } TempData["alert"] = string.Format("{0} has not been saved"); } repo.SaveLog(log); Location retLoc = repo.Locations.FirstOrDefault(p => p.Id == loc.Id); return View(retLoc); }
/************************************** Save functions *************************************/ public void SaveLocation(Location loc) { if (loc.Id == 0) { context.Locations.Add(loc); } else { Location dbEntry = context.Locations.Find(loc.Id); if (dbEntry != null) { dbEntry.Name = loc.Name; dbEntry.Subtitle = loc.Subtitle; dbEntry.Address1 = loc.Address1; dbEntry.Address2 = loc.Address2; dbEntry.City = loc.City; dbEntry.State = loc.State; dbEntry.Zip = loc.Zip; dbEntry.Latitude = loc.Latitude; dbEntry.Longitude = loc.Longitude; dbEntry.Description = loc.Description; dbEntry.ManagerName = loc.ManagerName; dbEntry.NoAtm = loc.NoAtm; dbEntry.AtmOnly = loc.AtmOnly; } } context.SaveChanges(); SaveLocationsXMLFile(); }