Exemple #1
0
        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");
        }
Exemple #2
0
        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");
        }