public async Task Create_ShouldReturnRedirect_OnException() { var service = new Mock <IStoryService>(); service.Setup(s => s.CreateAsync("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", user)) .Throws(new Exception()); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; CreateStoryInputModel model = new CreateStoryInputModel { Title = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." }; var result = await controller.Create(model); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public async Task Create_ShouldReturnView_OnInValidModel() { var service = new Mock <IStoryService>(); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; controller.ModelState.AddModelError("k", "e"); CreateStoryInputModel model = new CreateStoryInputModel { Title = "" }; var result = await controller.Create(model); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <CreateStoryInputModel>(); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public async Task <IActionResult> Create(CreateStoryInputModel model) { try { if (this.ModelState.IsValid) { string storyId = await storyService.CreateAsync(model.Title, CurrentUser); if (string.IsNullOrEmpty(storyId)) { AddDangerNotification(string.Format(Notifications.CreatedFail, model.Title)); return(Redirect("/Stories")); } logger.LogInformation( string.Format(SetLog.CreatedSuccess, CurrentUser.UserName, CurrentController, storyId )); AddWarningNotification(string.Format(Notifications.CreatedSuccess, model.Title)); return(Redirect($"/Stories/Update/{storyId}")); } else { logger.LogInformation(string.Format(SetLog.CreatedFail, CurrentUser.UserName, CurrentController)); AddDangerNotification(string.Format(Notifications.CreatedFail, model.Title)); return(View(model)); } } catch (System.Exception e) { logger.LogError(string.Format(SetLog.Error, CurrentUser.UserName, CurrentController, e.Message)); AddDangerNotification(string.Format(Notifications.Fail)); return(Redirect("/Stories")); } }
public void should_insert_story() { var inputModel = new CreateStoryInputModel { Name = "Story1", Status = "Backlog", PointValue = 1 }; MockFor<IStoryListRepository>() .Expect(r => r.Insert(new Story { Name = inputModel.Name, Status = inputModel.Status, PointValue = inputModel.PointValue })); ClassUnderTest.Execute(inputModel); VerifyCallsFor<IStoryListRepository>(); }
public IViewComponentResult Invoke() { var model = new CreateStoryInputModel(); return(View(model)); }