public void CreatePost_WithExistingPlanetName_ShouldReturnView() { // Arrange Mock <IPlanetService> planetService = new Mock <IPlanetService>(); planetService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(true); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); string errorMessage = null; tempData .SetupSet(t => t[WebConstants.TempDataErrorMessage] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); PlanetFormServiceModel formModel = this.GetPlanetFormModel(); PlanetsController planetsController = new PlanetsController(planetService.Object); planetsController.TempData = tempData.Object; // Act IActionResult result = planetsController.Create(1, formModel); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <PlanetFormServiceModel>(model); PlanetFormServiceModel returnModel = model as PlanetFormServiceModel; this.AssertPlanets(formModel, returnModel); Assert.Equal(string.Format(WebConstants.EntryExists, Planet), errorMessage); }
public void CreateGet_ShouldReturnView() { // Arrange PlanetsController planetsController = new PlanetsController(null); // Act IActionResult result = planetsController.Create(); // Assert Assert.IsType <ViewResult>(result); }
public void CreatePost_WithSuccessfullyCreatedPlanet_ShouldReturnRedirectResult() { // Arrange Mock <IPlanetService> planetService = new Mock <IPlanetService>(); const int discoveryId = 1; planetService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(false); planetService .Setup(s => s.Create(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <double>())) .Returns(true); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); string successmessage = null; tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessage] = It.IsAny <string>()) .Callback((string key, object message) => successmessage = message as string); PlanetFormServiceModel formModel = this.GetPlanetFormModel(); PlanetsController planetsController = new PlanetsController(planetService.Object); planetsController.TempData = tempData.Object; // Act IActionResult result = planetsController.Create(discoveryId, formModel); // Assert Assert.IsType <RedirectToActionResult>(result); RedirectToActionResult redirectResult = result as RedirectToActionResult; this.AssertRedirect(discoveryId, redirectResult); Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Planet, WebConstants.Added), successmessage); }
public void CreatePost_WithNotSuccessfullyCreatedPlanet_ShouldReturnBadRequest() { // Arrange Mock <IPlanetService> planetService = new Mock <IPlanetService>(); planetService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(false); planetService .Setup(s => s.Create(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(false); PlanetsController planetsController = new PlanetsController(planetService.Object); planetsController.TempData = Mock.Of <ITempDataDictionary>(); PlanetFormServiceModel formModel = this.GetPlanetFormModel(); // Act IActionResult result = planetsController.Create(1, formModel); // Assert Assert.IsType <BadRequestResult>(result); }