Inheritance: MyStoryController
Esempio n. 1
0
        public void Delete_Method_Should_Return_Httpnotfoubdresult()
        {
            // Arrange
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            // Act
            _controller = new PostController();
            var result = _controller.Delete(1000) as HttpNotFoundResult;

            // Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType(typeof(HttpNotFoundResult));
        }
Esempio n. 2
0
        public void Delete_Method_Should_Delete_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext();

            // Act
            var result = _controller.Delete(1) as RedirectToRouteResult;

            // Assert
            result.RouteValues["controller"].ShouldEqual("Home");
            result.RouteValues["action"].ShouldEqual("Index");

            _dbContext.Posts.Count().ShouldEqual(0);
        }
Esempio n. 3
0
        public void Delete_Method_Should_Delete_Post_Via_Ajax()
        {
            // Arrange
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext(isAjaxRequest:true);

            // Act
            var result = _controller.Delete(1) as JsonResult;

            // Assert
            var serializer = new JavaScriptSerializer();
            var output = serializer.Serialize(result.Data);
            Assert.AreEqual(@"{""success"":true}", output);

            _dbContext.Posts.Count().ShouldEqual(0);
        }
Esempio n. 4
0
        public void Detail_Method_Should_Return_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext(true);

            // Act
            var result = _controller.Detail(1) as ViewResult;

            // Assert
            result.ViewName.ShouldEqual("Detail");
            var model = result.Model as PostDetailViewModel;
            model.ShouldNotBeNull();
            model.Title.Contains(FunctionalTestHelper.PostTitle);
            model.Content.Contains(FunctionalTestHelper.PostContent);
        }
Esempio n. 5
0
        public void Valid_Model_Should_Be_Saved()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);

            var controllerContext = new Mock<ControllerContext>();
            controllerContext.SetupGet(x => x.HttpContext.Request.IsAuthenticated).Returns(true);
            controllerContext.SetupGet(x => x.HttpContext.User.Identity.Name).Returns("*****@*****.**");

            _controller = new PostController(new TagService());
            _controller.ControllerContext = controllerContext.Object;

            var postInput = new PostInput
            {
                Title = "title",
                Content = "content",
            };

            // Act
            var result = _controller.Write(postInput) as RedirectToRouteResult;

            // Assert
            _dbContext.Posts.Count().ShouldEqual(1);
            result.RouteValues["controller"].ShouldEqual("Home");
            result.RouteValues["action"].ShouldEqual("Index");
        }
Esempio n. 6
0
        public void Invalid_Model_Should_Not_Save()
        {
            // Arrange
            _controller = new PostController();
            _controller.ModelState.AddModelError("modelerror", "modelerror");
            var postInput = new PostInput();

            // Act
            var result = _controller.Write(postInput) as ViewResult;

            // Assert
            result.ViewData.ModelState.IsValid.ShouldBeFalse();
            result.ViewName.ShouldEqual("Write");
        }
Esempio n. 7
0
        public void Edit_Methood_Should_Return_Postmodel()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(x => x.HttpContext.Request.IsAuthenticated).Returns(true);
            mock.SetupGet(x => x.HttpContext.User.Identity.Name).Returns(FunctionalTestHelper.AccountEmail);

            _controller = new PostController();
            _controller.ControllerContext = mock.Object;

            // Act
            var result = _controller.Edit(1) as ViewResult;
            var post = result.Model as PostInput;

            // Assert
            post.Title.ShouldEqual(FunctionalTestHelper.PostTitle);
            post.Content.ShouldEqual(FunctionalTestHelper.PostContent);
            "Edit".ShouldEqual(result.ViewName);
        }
Esempio n. 8
0
        public void Edit_Method_Should_Validate_Model()
        {
            // Arrange
            _controller = new PostController();
            _controller.ModelState.AddModelError("modelerror", "modelerror");

            var result = _controller.Edit(new PostInput()) as ViewResult;
            result.ViewName.SequenceEqual("Edit");
            result.ViewData.ModelState.IsValid.ShouldBeFalse();
        }
Esempio n. 9
0
        public void Edit_Method_Should_Save_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController(new TagService());
            _controller.SetFakeControllerContext();

            // valueprovider is needed for TryUpdateModel() method
            FormCollection form = new FormCollection();
            _controller.ValueProvider = form.ToValueProvider();

            var input = new PostInput
            {
                Content = "edited content",
                Id = 1,
                Title = "edited title",
            };

            // Act
            var result = _controller.Edit(input) as RedirectToRouteResult;

            // Assert
            result.RouteValues["controller"].ShouldEqual("Post");
            result.RouteValues["action"].ShouldEqual("Detail");
            var post = _dbContext.Posts.SingleOrDefault(p => p.Id == 1);
            post.ShouldNotBeNull();
            post.Title.Contains("edited");
            post.Content.Contains("edited");
        }
Esempio n. 10
0
        public void Detail_Method_Should_Validate_Model()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            // Act
            _controller = new PostController();
            var result = _controller.Detail(1000) as HttpNotFoundResult;

            // Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType(typeof(HttpNotFoundResult));
        }