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 EditGet() { var controller = new LocationsController(); var result = controller.Edit(2); Assert.IsNotNull(result); }
public void CannotEditLocationsThatDoNotExist() { var locationsController = new LocationsController(null, Context, null); var id = 9; var result = locationsController.Edit(id) as ViewResult; Assert.Equal($"No location with id {id} exists. Please select a different location to edit", result.ViewData["Error"]); }
public void Location_Can_Edit_Valid_Changes() { // Arrange Mock <ILogger <LocationsController> > mockLogger = new Mock <ILogger <LocationsController> >(); Mock <ILocationRepository> mockLocationRepository = new Mock <ILocationRepository>(); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); mockLocationRepository.Setup(m => m.GetLocations).Returns(new Location[] { new Location { LocationId = 1, ParentId = 0, Description = "L1" }, new Location { LocationId = 2, ParentId = 1, Description = "L2" }, new Location { LocationId = 3, ParentId = 1, Description = "L3" } }.AsQueryable <Location>()); LocationsController controller = new LocationsController(mockLogger.Object, mockLocationRepository.Object) { TempData = tempData.Object }; // Act IActionResult result1 = controller.Edit(new Location { LocationId = 1, ParentId = 0, Description = "L1" }); IActionResult result2 = controller.Edit(new Location { LocationId = 2, ParentId = 1, Description = "L2" }); IActionResult result3 = controller.Edit(new Location { LocationId = 3, ParentId = 1, Description = "L3" }); // Assert Assert.AreEqual("List", (result1 as RedirectToActionResult).ActionName); Assert.AreEqual("List", (result2 as RedirectToActionResult).ActionName); Assert.AreEqual("List", (result3 as RedirectToActionResult).ActionName); }
public void Location_Cannot_Edit_Nonexistent() { // Arrange Mock <ILogger <LocationsController> > mockLogger = new Mock <ILogger <LocationsController> >(); Mock <ILocationRepository> mockLocationRepository = new Mock <ILocationRepository>(); LocationsController controller = new LocationsController(mockLogger.Object, mockLocationRepository.Object); // Act Location result = GetViewModel <Location>(controller.Edit(4)); // Assert Assert.IsNull(result); }
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); }
public async Task CannotMakeExistingLocationsInvalidAsync() { var goodLocation = new LocationModel { Name = "Prime Spot", StreetAddress = "777 E Wisconsin Ave", City = "Milwaukee", State = "WI", ZipCode = "53202" }; var mockGeocoder = new Mock <IGeocoder>(); var geocodeWithGoodAddress = new GoogleGeocodeResponse { results = new List <Result> { new Result { formatted_address = "This is a nicely formatted address." } } }; mockGeocoder.Setup(g => g.GetGeocodeAsync(goodLocation)).Returns(Task.FromResult(geocodeWithGoodAddress)); var locationsController = new LocationsController(null, Context, mockGeocoder.Object); var response = await locationsController.Create(goodLocation) as ViewResult; var badLocation = new LocationModel { Name = goodLocation.Name, StreetAddress = goodLocation.StreetAddress, City = goodLocation.City, State = goodLocation.State, ZipCode = "99999" // Invalid Zip Code }; var badGeocode = new GoogleGeocodeResponse { results = Enumerable.Empty <Result>().ToList() }; mockGeocoder.Setup(g => g.GetGeocodeAsync(badLocation)).Returns(Task.FromResult(badGeocode)); response = await locationsController.Edit(1, badLocation) as ViewResult; Assert.Equal( "This address could not be found. Please check this address and try again!", response.ViewData["Error"]); }
public void Post_MethodEditsItem_Test() { DBSetUp(); LocationsController controller = new LocationsController(mock.Object); Location testLocation = new Location(); testLocation.LocationName = "Lima, Peru"; controller.Create(testLocation); testLocation.LocationName = "Bejing, China"; controller.Edit(testLocation); Location expectedLocation = new Location(); expectedLocation.LocationName = "Bejing, China"; ViewResult indexView = new LocationsController(mock.Object).Index() as ViewResult; var collection = indexView.ViewData.Model as IEnumerable <Location>; Assert.Equal(expectedLocation.LocationName, (collection.FirstOrDefault(c => c.LocationId == testLocation.LocationId)).LocationName); }
public void EditPost() { var controller = new LocationsController(); // BracketMapping bracket = new BracketMapping(); List <BracketMapping> bracketMappings = new List <BracketMapping>(); var location = new Location() { Id = 1, Name = "Ellensburg", BracketMappings = new List <BracketMapping>() }; var result = controller.Edit(location, bracketMappings); Assert.IsNotNull(result); // Assert.AreEqual("Index", result.RouteValues["action"]); }
public void Location_Cannot_Edit_Invalid_Changes() { // Arrange Location Location = new Location { LocationId = 2, ParentId = 1, Description = "" }; Mock <ILogger <LocationsController> > mockLogger = new Mock <ILogger <LocationsController> >(); Mock <ILocationRepository> mockLocationRepository = new Mock <ILocationRepository>(); LocationsController controller = new LocationsController(mockLogger.Object, mockLocationRepository.Object); controller.ModelState.AddModelError("error", "error"); // Act IActionResult result = controller.Edit(Location); // Assert mockLocationRepository.Verify(m => m.EditLocation(It.IsAny <Location>()), Times.Never()); }
public async Task Edit_LocationName_ServiceGetsHitWithRightName() { // Arrange ILocationService service = Substitute.For <ILocationService>(); service.GetByName(Arg.Any <string>()).Returns(new Location { id = "1", Name = "Contoso", Address = new Address { Text = "abc", ZipOrPostcode = "54321" } }); LocationsController controller = GetController(locationService: service); string id = "Contoso"; // Act await controller.Edit(id); // Assert #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed service.Received(1).GetByName(id); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }
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 TestLocationEditView() { // Arrange var location = GetLocation(); location.Name = "Store 4"; var mockLocationService = new Mock <ILocationService>(); var mockPostCodeService = new Mock <IPostCodeService>(); mockLocationService.Setup(r => r.GetById(3)).Returns(location); mockLocationService.Setup(r => r.Update(location)); var controller = new LocationsController(mockLocationService.Object, mockPostCodeService.Object); // Act var result = controller.Edit(3); // Assert var locationTypeEx = LocationTypeEx.LocationTypes.First(lte => lte.LocationType == (int)location.Type); Assert.AreEqual(location.Name, ((LocationsDetailsViewModel)((ViewResult)result).Model).Name); Assert.AreEqual(locationTypeEx, ((LocationsDetailsViewModel)((ViewResult)result).Model).LocationType); }
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)); }