public void CreatePost_WithExistingTelescopeName_ShouldReturnView() { // Arrange Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>(); telescopeService .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); TelescopeFormServiceModel formModel = this.GetTelescopeFormModel(); TelescopesController telescopesController = new TelescopesController(telescopeService.Object); telescopesController.TempData = tempData.Object; // Act IActionResult result = telescopesController.Create(formModel); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <TelescopeFormServiceModel>(model); TelescopeFormServiceModel returnModel = model as TelescopeFormServiceModel; this.AssertTelescopes(formModel, returnModel); Assert.Equal(string.Format(WebConstants.EntryExists, Telescope), errorMessage); }
public void CreatePost_WithNotSuccessfullyCreatedTelescope_ShouldReturnBadRequest() { // Arrange Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>(); telescopeService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(false); telescopeService .Setup(s => s.Create( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <double>(), It.IsAny <string>())) .Returns(-1); TelescopeFormServiceModel formModel = this.GetTelescopeFormModel(); TelescopesController telescopesController = new TelescopesController(telescopeService.Object); telescopesController.TempData = Mock.Of <ITempDataDictionary>(); // Act IActionResult result = telescopesController.Create(formModel); // Assert Assert.IsType <BadRequestResult>(result); }
public void CreateGet_ShouldReturnView() { // Arrange TelescopesController telescopesController = new TelescopesController(null); // Act IActionResult result = telescopesController.Create(); // Assert Assert.IsType <ViewResult>(result); }
public void CreatePost_WithSuccessfullyCreatedTelescope_ShouldReturnRedirectResult() { // Arrange Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>(); const int telescopeId = 1; telescopeService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(false); telescopeService .Setup(s => s.Create( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <double>(), It.IsAny <string>())) .Returns(telescopeId); 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); TelescopeFormServiceModel formModel = this.GetTelescopeFormModel(); TelescopesController telescopesController = new TelescopesController(telescopeService.Object); telescopesController.TempData = tempData.Object; // Act IActionResult result = telescopesController.Create(formModel); // Assert Assert.IsType <RedirectToActionResult>(result); RedirectToActionResult redirectResult = result as RedirectToActionResult; this.AssertRedirect(telescopeId, redirectResult); Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Telescope, WebConstants.Added), successmessage); }