public async Task LocationsControllerShouldNotPersistTheSameLocationTwice() { var location = 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(location)).Returns(Task.FromResult(geocodeWithGoodAddress)); var locationsController = new LocationsController(null, Context, mockGeocoder.Object); var result = await locationsController.Create(location) as ViewResult; result = await locationsController.Create(location) as ViewResult; Assert.Equal("The given address already exists. Enter a new address", result.ViewData["Error"]); }
public void LocationsController_Create_Post_Invalid_Model_Should_Send_Back_For_Edit() { // Arrange var mock = new Mock <LocationsBackend>(context); var controller = new LocationsController(context, mock.Object); // Get mock data as view model var dataMock = new LocationsDataMock(); var dataRaw = dataMock.GetAllViewModelList()[0]; var data = new AllTablesViewModel(dataRaw); // Make ModelState Invalid controller.ModelState.AddModelError("test", "test"); // Act var result = controller.Create(data) as ViewResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("Create Post", result.ViewName); }
public async Task LocationsControllerDoesNotReturnAddressIfAddressNotFound() { var badLocation = new LocationModel { Name = "Imaginary Spot", StreetAddress = "1313 Mockingbird Ln", City = "Keflavik", State = "CZ", ZipCode = "98765" }; var badGeocode = new GoogleGeocodeResponse { results = Enumerable.Empty <Result>().ToList() }; var mockGeocoder = new Mock <IGeocoder>(); mockGeocoder.Setup(g => g.GetGeocodeAsync(badLocation)).Returns(Task.FromResult(badGeocode)); var locationsController = new LocationsController(null, Context, mockGeocoder.Object); var result = await locationsController.Create(badLocation) as ViewResult; Assert.Equal( "This address could not be found. Please check this address and try again!", result.ViewData["Error"]); }
public void CreateGet() { var controller = new LocationsController(); var result = controller.Create(); Assert.IsNotNull(result); }
public async Task ValidModel_CreateNewItemInDbAndReturnToIndex() { // Arrange Location savedLocation = null; _mockRepo.Setup(repo => repo.CreateAsync(It.IsAny <Location>())) .ReturnsAsync(true) .Callback <Location>(x => savedLocation = x); // Act var result = await _sut.Create(_newLocation); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); _mockRepo.Verify(repo => repo.CreateAsync(It.IsAny <Location>()), Times.Once); Assert.Equal("Index", redirectToActionResult.ActionName); Assert.Equal(_newLocation.LocationName, savedLocation.LocationName); }
public async Task LocationsControllerShouldNotPersistTheSameLocationTwice() { var location = new LocationModel { Name = "Prime Spot", StreetAddress = "777 E Wisconsin Ave", City = "Milwaukee", State = "WI", ZipCode = "53202" }; var locationsController = new LocationsController(Configuration, Context); var result = await locationsController.Create(location) as ViewResult; result = await locationsController.Create(location) as ViewResult; Assert.Equal("The given address already exists. Enter a new address", result.ViewData["Error"]); }
//Test POST for creating new location that is null public void LocationController_Create_ReturnsBadRequest_WhenObjectIsNull() { //Arrange new ObjectResult Location location = null; //Act var result = controller.Create(location); //Assert Assert.IsType <BadRequestObjectResult>(result); }
public void CreatePost() { var controller = new LocationsController(); var location = new Location() { Name = "New car shop" }; var result = controller.Create(location); Assert.IsNotNull(result); }
public void Post_MethodAddsItem_Test() { DBSetUp(); LocationsController controller = new LocationsController(mock.Object); Location testLocation = new Location(); testLocation.LocationName = "Lima, Peru"; controller.Create(testLocation); ViewResult indexView = new LocationsController(mock.Object).Index() as ViewResult; var collection = indexView.ViewData.Model as IEnumerable <Location>; Assert.Contains <Location>(testLocation, collection); }
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 DB_CreateNewEntry_Test() { // Arrange LocationsController controller = new LocationsController(db); Location testLocation = new Location(); testLocation.Description = "TestDb Location"; testLocation.Name = "TestDb Name"; // Act controller.Create(testLocation); var collection = (controller.Index() as ViewResult).ViewData.Model as IEnumerable <Location>; // Assert Assert.Contains <Location>(testLocation, collection); }
public void Locations_Create_Get_Default_Should_Pass() { // Arrange var mock = new Mock <LocationsBackend>(context); var dataMock = new LocationsDataMock(); var myController = new LocationsController(context, mock.Object); // Act var result = myController.Create(); // Assert Assert.IsNotNull(result); }
public async Task LocationsControllerDoesNotReturnAddressIfAddressNotFound() { var badLocation = new LocationModel { Name = "Imaginary Spot", StreetAddress = "1313 Mockingbird Ln", City = "Keflavik", State = "CZ", ZipCode = "98765" }; var locationsController = new LocationsController(Configuration, Context); var result = await locationsController.Create(badLocation) as ViewResult; Assert.Equal( "This address could not be found. Please check this address and try again!", result.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 Create_PostRequest_EnsureRedirectionToIndex() { var mockLocationRepo = new Mock <ILocationRepository>(); mockLocationRepo.Setup(repo => repo.Create(null)); var controller = new LocationsController(mockLocationRepo.Object, null, null); var locationViewModel = new LocationViewModel() { Name = "Walmart" }; // Act var result = controller.Create(locationViewModel, null); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Index", redirectToActionResult.ActionName); }
public void Location_Cannot_Add_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.Create(Location); // Assert mockLocationRepository.Verify(m => m.AddLocation(It.IsAny <Location>()), Times.Never()); }
public async void CreatelocationReturnsLocation() { //A -arrange var location1 = new Location(); location1.ID = 1; location1.Name = "Cici"; var mockRepo = new Mock <ILocationRepository>(); mockRepo.Setup(repo => repo.CreateAsync(It.IsAny <Location>())) .Returns(Task.FromResult(location1)) .Verifiable(); var controller = new LocationsController(mockRepo.Object); //A -act var result = await controller.Create(location1); //A -assert var viewResult = Assert.IsType <Location>((result as ObjectResult).Value as Location); Assert.Equal(viewResult.Name, "Cici"); Assert.Equal((result as ObjectResult).StatusCode, 201); }
public void TestLocationCreateView() { // Arrange var location = new Location { Name = "Store test", Type = LocationType.Megastore }; var mockLocationService = new Mock <ILocationService>(); var mockPostCodeService = new Mock <IPostCodeService>(); mockLocationService.Setup(r => r.Add(location)); var controller = new LocationsController(mockLocationService.Object, mockPostCodeService.Object); // Act var result = controller.Create(GetViewModel(0)); // 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); }
//Test POST for creating new location public void LocationController_Create_ReturnsObject_WhenNewObject() { controller = createContext(controller); //Arrange new ObjectResult var location = new Location() { Id = 4, Country = "Country4", City = "City4", ZipCode = "ZipCode4", PrimaryLine = "PrimaryLine4", SecondaryLine = "SecondaryLine4", }; //Act var result = controller.Create(location); var resultAsObject = result as ObjectResult; var resultObject = resultAsObject.Value as Location; //Assert Assert.IsType <ObjectResult>(result); Assert.Equal(location.Country, resultObject.Country); }
public void LocationsController_Create_Post_Valid_Model_Should_Pass() { // Arrange var mock = new Mock <LocationsBackend>(context); mock.Setup(backend => backend.Create(It.IsAny <AllTablesViewModel>())).Returns(true); var controller = new LocationsController(context, mock.Object); // Get mock data as view model var dataMock = new LocationsDataMock(); var dataRaw = dataMock.GetAllViewModelList()[0]; var data = new AllTablesViewModel(dataRaw); // Act var result = controller.Create(data) as RedirectToActionResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("Index", result.ActionName); }
public void Location_Can_Add_Valid_Changes() { // Arrange Location Location = new Location { LocationId = 2, ParentId = 1, Description = "Test" }; Mock <ILogger <LocationsController> > mockLogger = new Mock <ILogger <LocationsController> >(); Mock <ILocationRepository> mockLocationRepository = new Mock <ILocationRepository>(); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); LocationsController controller = new LocationsController(mockLogger.Object, mockLocationRepository.Object) { TempData = tempData.Object }; // Act IActionResult result = controller.Create(Location); // Assert mockLocationRepository.Verify(m => m.AddLocation(Location)); Assert.AreEqual("List", (result as RedirectToActionResult).ActionName); }