Пример #1
0
        public void PostNewView()
        {
            var controller = new PostController();
            var actionResult = controller.New();

            Assert.IsNotNull(actionResult);
        }
		public void Setup()
		{
			UnitOfWork = TestHelper.TestHelper.GetMockUnitOfWork();
			PostDbSet = TestHelper.TestHelper.GetMockPostDbSet();
			Posts = TestHelper.TestHelper.GetFakePosts();
			TagDbSet = TestHelper.TestHelper.GetMockTagDbSet();

			PostController = new PostController(UnitOfWork.Object, PostDbSet.Object, TagDbSet.Object);
		}
        public Post TestGetByTModel()
        {
            var name = TestAdd();
            PostController pc = new PostController();

            var existing = pc.Get(new Post() { Name = name });
            Assert.IsNotNull(existing);

            return existing;
        }
        public void Edit_PostRequestCallsEditAndRedirects()
        {
            var repo = Mock.Create<IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Edit(Arg.IsAny<string>(), Arg.IsAny<Post>())).MustBeCalled();

            var result = controller.Edit("Foo", new Post() { Id = "test-post-2" });

            Mock.Assert(repo);
            Assert.IsTrue(result is RedirectToRouteResult);
        }
        public void Edit_PostRequestNotFoundResult()
        {
            var id = "test-post";
            var repo = Mock.Create<IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Get(id)).Returns((Post)null);

            var result = controller.Edit(id, new Post());

            Assert.IsTrue(result is HttpNotFoundResult);
        }
Пример #6
0
        public void CanNotUpdatePost()
        {
            //Arrange
            _repository.Setup(p => p.GetAllPostByBlogId(1)).Returns(testPosts);
            PostController controller = new PostController(_repository.Object, _manager);

            //Act
            Post result = (Post)controller.Edit(4).ViewData.Model;

            //Assert
            Assert.IsNull(result);
        }
        public void Edit_GetRequestSendsPostToView()
        {
            var id = "test-post";
            var repo = Mock.Create<IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Get(id)).Returns(new Post {Id = id});

            var result = (ViewResult) controller.Edit(id);
            var model = (Post) result.Model;

            Assert.AreEqual(id, model.Id);
        }
Пример #8
0
        public void CreateAddsPost()
        {
            //Arrange
            Post post = new Post { PostId = 1, Blog = new Blog { BlogId = 1, BlogTitel = "First test-blog", Closed = false }, PostTitle = "Post #1", Text = "First test-post", Date = new DateTime() };
            var controller = new PostController(_repository.Object, _manager);

            //Act
            ActionResult result = controller.Edit(post);

            //Assert
            _repository.Verify(b => b.AddPost(post, 1));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Пример #9
0
        public void CreateDoesNotAddPost()
        {
            //Arrange
            Post post = new Post { PostId = 1, Blog = new Blog { BlogId = 1, BlogTitel = "First test-blog", Closed = false }, PostTitle = "Post #1", Text = "First test-post", Date = new DateTime() };
            var controller = new PostController(_repository.Object, _manager);
            controller.ModelState.AddModelError("error", "error");

            //Act
            ActionResult result = controller.Edit(post);

            //Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
        public void TestUpdate()
        {
            var existing = TestGetByTModel();
            existing.Title = "Updated title";

            var pc = new PostController();
            pc.Update(existing);

            var updated = pc.Get(existing.Name);

            Assert.AreEqual(existing.Title, updated.Title);

        }
Пример #11
0
        public void CanUpdatePost()
        {
            //Arrange
            _repository.Setup(p => p.GetAllPostByBlogId(1)).Returns(testPosts);
            PostController controller = new PostController(_repository.Object, _manager);

            //Act
            Post firstPost = (Post)controller.Edit(1).ViewData.Model;
            Post secondPost = (Post)controller.Edit(2).ViewData.Model;

            //Assert
            Assert.AreEqual(1, firstPost.PostId);
            Assert.AreEqual(2, secondPost.PostId);
        }
Пример #12
0
        public void CanUpdatePost()
        {
            //Arrange
            _repository.Setup(p => p.GetAllPostByBlogId(1)).Returns(testPosts);
            PostController controller = new PostController(_repository.Object, _manager);

            //Act
            Post firstPost  = (Post)controller.Edit(1).ViewData.Model;
            Post secondPost = (Post)controller.Edit(2).ViewData.Model;

            //Assert
            Assert.AreEqual(1, firstPost.PostId);
            Assert.AreEqual(2, secondPost.PostId);
        }
Пример #13
0
        public void GivenAPostController_WhenIIAskToShowASinglePost_ThenItReturnsTheCorrectView()
        {
            var postRepositoryMock = new Mock<IPostRepository>();

            postRepositoryMock.Setup(
                r =>
                r.GetBlogPosts(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>()))
                .Returns(new List<Post> {new Post {Title = "title"}});
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);
            var result =
                controller.Show(new PostLinkViewModel {Year = 1, Month = 1, Day = 1, Link = "notempty"}) as ViewResult;

            Assert.That(result, Is.Not.Null);
        }
Пример #14
0
        public void PostNewViewCompleteModel()
        {
            var controller = new PostController { ControllerContext = ControllerCtx };
            var form = new FormCollection
                {
                    {"Title", "Hello world"},
                    {"Text", "This is the first post."}
                };
            var postNew = BindModel<PostModel>(controller, form);
            var actionResult = controller.New(postNew);

            Assert.IsNotNull(actionResult);
            Assert.AreEqual(typeof(RedirectToRouteResult), actionResult.GetType());
        }
Пример #15
0
        public void DeleteCommentOrLike_Failed()
        {
            int postCommentOrLikeId = 1;

            _postsRepoMock.Setup(m => m.DeleteComment(postCommentOrLikeId, _loginUser.UserId)).Returns(false);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.DeleteCommentOrLike(postCommentOrLikeId);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "ERROR");
        }
Пример #16
0
        public void Edit_PostRequestNotFoundResult()
        {
            var id = "test-post";

            var repo       = Mock.Create <IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Get(id)).
            Returns((Post)null);

            var result = controller.Edit(id, new Post());

            Assert.IsTrue(result is HttpNotFoundResult);
        }
Пример #17
0
        public void PostNewViewIncompleteModel()
        {
            var controller = new PostController {ControllerContext = ControllerCtx};
            var form = new FormCollection
                {
                    {"Title", ""},
                    {"Text", ""}
                };
            var postNew = BindModel<PostModel>(controller, form);
            var actionResult = controller.New(postNew);

            Assert.IsNotNull(actionResult);
            Assert.AreEqual(typeof(ViewResult), actionResult.GetType());
        }
Пример #18
0
        public void Unlike()
        {
            int postId = 2;

            _postsRepoMock.Setup(m => m.DeleteLike(postId, _loginUser.UserId)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.Unlike(postId);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "OK");
        }
Пример #19
0
        public void CheckIfAnyMoreComments_Failed()
        {
            int postId = 2;

            _postsRepoMock.Setup(m => m.GetCommentsAndLikes(postId)).Returns(new List <PostCommentOrLike>());
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.ChekIfAnyMoreComments(postId);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is bool);
            Assert.IsFalse((bool)result.Data);
        }
Пример #20
0
        public static void Remove()
        {
            try
            {
                while (true)
                {
                    Console.Write("Type a valid ID: ");

                    Int32  id    = 0;
                    String input = Console.ReadLine();

                    if (!Int32.TryParse(input, out id))
                    {
                        Console.WriteLine("Type a valid number.");
                        continue;
                    }

                    Post post = new PostController().Get(new Post()
                    {
                        PostId = id
                    });

                    if (post == null)
                    {
                        Console.WriteLine("Not found. Continue[y/n]");
                        String op = Console.ReadLine();

                        if (op.ToLower().Equals("y"))
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }

                    new PostController().Remove(post);
                    Console.WriteLine("\nRecord removed.");
                    break;
                }
            }
            catch (Exception e)
            {
                Message.Error(e);
            }

            Console.ReadKey();
        }
Пример #21
0
        public async Task Create_ShouldReturn_BadRequest_ForInvalidModel()
        {
            var mock       = new Mock <IPostRepository>();
            var controller = new PostController(mock.Object);

            controller.ModelState.AddModelError("error", "erro occured");

            var actionResult = await controller.Create(null);

            Assert.IsNotNull(actionResult);

            BadRequestObjectResult result = actionResult as BadRequestObjectResult;

            Assert.IsNotNull(result);
        }
Пример #22
0
        public void PostNewTest()
        {
            //var testProducts = GetTestProducts();
            var controller = new PostController();
            var post       = GetNewPost();

            // Act
            IHttpActionResult actionResult = controller.Post(post);
            var createdResult = actionResult as CreatedAtRouteNegotiatedContentResult <Post>;

            // Assert
            Assert.IsNotNull(createdResult);
            Assert.AreEqual("DefaultApi", createdResult.RouteName);
            Assert.IsNotNull(createdResult.RouteValues["id"]);
        }
Пример #23
0
        public async void Task_Add_InvalidData_Return_BadRequest()
        {
            //Arrange
            var  controller = new PostController(repository);
            Post post       = new Post()
            {
                Title = "Test Title More Than 20 Characteres", Description = "Test Description 3", CategoryId = 3, CreatedDate = DateTime.Now
            };

            //Act
            var data = await controller.AddPost(post);

            //Assert
            Assert.IsType <BadRequestResult>(data);
        }
Пример #24
0
        public async void Task_Add_ValidData_Return_OkResult()
        {
            //Arrange
            var controller = new PostController(repository);
            var post       = new Post()
            {
                Title = "Test Title 3", Description = "Test Description 3", CategoryId = 2, CreatedDate = DateTime.Now
            };

            //Act
            var data = await controller.AddPost(post);

            //Assert
            Assert.IsType <OkObjectResult>(data);
        }
Пример #25
0
        public void ListBySearchSetsSearchContainerOnModel()
        {
            FakePostService postService = new FakePostService();

            PostController controller = new PostController(postService, null, null, null);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(new FakeHttpContext("~/"), new RouteData(), controller);

            OxiteModelList <Post> result = controller.ListBySearch(null, 0, new SearchCriteria()
            {
                Term = "test"
            }, null);

            Assert.IsType <SearchPageContainer>(result.Container);
        }
Пример #26
0
        public void PostControllerDetailsReturnsView_ExpectedSuccess()
        {
            // Arrange
            Mock <IPostLogic> mockPostLogic = new Mock <IPostLogic>();

            mockPostLogic.Setup(x => x.GetSinglePost(It.IsAny <Int32>())).ReturnsAsync(GetPost());
            var controllerUnderTest = new PostController(_context, mockPostLogic.Object);

            // Act
            var result = controllerUnderTest.Details(1);

            // Assert
            Assert.NotNull(result);
            var viewResult = Assert.IsType <Task <IActionResult> >(result);
        }
Пример #27
0
        //  [Fact]
        public void TestCreatePost()
        {
            var builder = new DbContextOptionsBuilder <PostContext>()
                          .UseInMemoryDatabase(databaseName: "database_name1");

            var context = new PostContext(builder.Options);
            var post    = new Post {
                id = 1, userid = 1, title = $"Sample{1}", body = "Wrox Press"
            };

            var controller = new PostController(context, CreateMockPostClientService());

            controller.Post(post);
            Assert.Equal(1, context.Posts.Count());
        }
Пример #28
0
        public async Task Edit_Post_ModelIsInvalid_AsksForEditView()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.EditAsync(It.IsAny <PostDTO>())).Throws(new ValidationException("", ""));
            Mock <DepartmentService> dmock = new Mock <DepartmentService>();

            dmock.Setup(m => m.GetAllAsync()).ReturnsAsync(new DepartmentDTO[] { new DepartmentDTO {
                                                                                 } });
            PostController controller = GetNewPostController(mock.Object, null, dmock.Object);

            ViewResult result = (await controller.Edit(new PostViewModel())) as ViewResult;

            Assert.AreEqual("Edit", result.ViewName);
        }
        public void Edit_PostRequestModelStateInvalidReturnsModelToView()
        {
            var id = "test-post";
            var repo = Mock.Create<IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Get(id)).Returns(new Post { Id = id });

            controller.ViewData.ModelState.AddModelError("key", "error message");

            var result = (ViewResult)controller.Edit(id, new Post() {Id = "test-post-2"});
            var model = (Post)result.Model;

            Assert.AreEqual("test-post-2", model.Id);
        }
Пример #30
0
        public async Task GetById_ShouldReturn_NotFound_ForAbsentItem(int id)
        {
            var mock = new Mock <IPostRepository>();

            mock.Setup(repo => repo.Find(id)).Returns(Task.FromResult((Post)null));
            var controller = new PostController(mock.Object);

            var actionResult = await controller.GetById(id);

            Assert.IsNotNull(actionResult);

            NotFoundResult result = actionResult as NotFoundResult;

            Assert.IsNotNull(result);
        }
Пример #31
0
        public void SavePost()
        {
            var postId          = 1;
            var postManagerMock = new Mock <IPostManager>();

            postManagerMock.Setup(pm => pm.Save(It.IsAny <PostDetailsModel>()));

            var controller = new PostController(postManagerMock.Object);

            controller.Put(new PostDetailsModel {
                Id = postId
            });

            postManagerMock.Verify(p => p.Save(It.IsAny <PostDetailsModel>()), Times.Once);
        }
Пример #32
0
        public void Edit_PostRequestCallsEditAndRedirects()
        {
            var repo       = Mock.Create <IPostRepository>();
            var controller = new PostController(repo);

            Mock.Arrange(() => repo.Edit(Arg.IsAny <string>(), Arg.IsAny <Post>()))
            .MustBeCalled();

            var result = controller.Edit("foo", new Post {
                Id = "test-post-2"
            });

            Mock.Assert(repo);
            Assert.IsTrue(result is RedirectToRouteResult);
        }
Пример #33
0
        public void PostControllerSendRequestReturnsView_ExpectedSuccess()
        {
            // Arrange
            var posts = GetPosts();
            var _mock = new Mock <IPostLogic>();

            _mock.Setup(x => x.GetAllPosts()).ReturnsAsync(posts);
            var controllerUnderTest = new PostController(_context, _mock.Object);

            // Act
            var result = controllerUnderTest.SendRequest(1);

            // Assert
            Assert.Equal(typeof(Task <IActionResult>), result.GetType());
        }
Пример #34
0
        public void Valid_Post_Model_Because_Have_Need_Values()
        {
            // Arrange
            Post model = new Post
            {
                Header    = "hello",
                ShortText = "short",
                FullText  = "full"
            };
            Mock <IPostRepository> mock   = new Mock <IPostRepository>();
            PostController         target = new PostController(mock.Object);

            // Assert
            Assert.IsTrue(Helpers.IsValidModel <Post>(model));
        }
Пример #35
0
        public void PutEntityBadRepositoryGetId()
        {
            var controller = new PostController(GetMockRepo(false));
            var entity     = new PostFormPut {
                Title = "test"
            };

            controller.SetClaimsPrincipal(1, "*****@*****.**", Role.Admin);

            // Act
            var result = controller.Put(1, entity);

            // Assert
            Assert.IsType <UnprocessableEntityObjectResult>(result);
        }
Пример #36
0
        public void Marked_with_invalidID()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.MarkPost(It.IsAny <int>())).Returns(false);
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.MarkPost(19);

            Assert.IsType <NotFoundResult>(response);
        }
Пример #37
0
        public void TestCreatePostWhenIdIsSet()
        {
            var mock = new Mock <PostDAO>();

            mock
            .Setup(dao => dao.Create(It.IsAny <Post>()))
            .Throws <ArgumentException>();

            var postController = new PostController(mock.Object);

            var actionResult = postController.CreatePost(post4);

            mock.Verify((dao) => dao.Create(post4));
            Assert.IsInstanceOf <BadRequestResult>(actionResult);
        }
Пример #38
0
        public void Annotation_valid()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetAnnotation(It.IsAny <int>())).Returns(new Post().Annotation);
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetAnnotation(19);

            Assert.IsType <OkObjectResult>(response);
        }
Пример #39
0
        public void GetQuestionWithAnswersByPostId_returns_ListOfDTO()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetQuestionWithAnswersByPostId(It.IsAny <int>())).Returns(new List <Post>());
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetQuestionWithAnswersByPostId(19);

            Assert.IsType <OkObjectResult>(response);
        }
Пример #40
0
        public void GetQuestionWithAnswersByPostId_INvalidID()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetQuestionWithAnswersByPostId(It.IsAny <int>())).Returns((List <Post>)null);
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetQuestionWithAnswersByPostId(17);

            Assert.IsType <NotFoundResult>(response);
        }
Пример #41
0
        public void GetPostsBySearchString_invalid()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetPostsBySearchString("", It.IsAny <int>(), It.IsAny <int>())).Returns((List <Post>)null);
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetPostsBySearchString("", 0, 0);

            Assert.IsType <NotFoundResult>(response);
        }
Пример #42
0
        public void GetPostsBySearchString()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetPostsBySearchString("sql", It.IsAny <int>(), It.IsAny <int>())).Returns(new List <Post>());
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetPostsBySearchString("sql", 17, 19);

            Assert.IsType <OkObjectResult>(response);
        }
Пример #43
0
        public void Annotation_failedupdate()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.UpdateAnnotation(It.IsAny <int>(), "")).Returns(false);
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.UpdateAnnotation(19, "");

            Assert.IsType <NotFoundResult>(response);
        }
Пример #44
0
        public void Annotation_none()
        {
            var dataServiceMock = new Mock <IDataService>();

            dataServiceMock.Setup(o => o.PostRepository.GetAnnotation(It.IsAny <int>())).Returns("No annotation for this post.");
            var urlHelperMock = new Mock <IUrlHelper>();

            var ctrl = new PostController(dataServiceMock.Object);

            ctrl.Url = urlHelperMock.Object;

            var response = ctrl.GetAnnotation(19);

            Assert.IsType <NotFoundResult>(response);
        }
        public string TestAdd()
        {
            var pc = new PostController();
            var uniqueName = "Post on " + DateTime.Now.ToString();
            pc.Add(new api.Models.Post()
            {
                Name = uniqueName,
                Title = "Test Item",
                Contents = "Test Contents"
            });
            int changes = pc.SaveChanges();
            Assert.AreEqual(1, changes);

            var all = pc.GetAll().Where(p => p.Name == uniqueName).ToList();
            var count = all.Count();
            Assert.AreEqual(1, count);
            return uniqueName;
        }
Пример #46
0
        public void IndexReturnsAllBlogPosts()
        {
            //Arrange
            //Defining get all method
            _repository.Setup(p => p.GetAllPostByBlogId(1)).Returns(testPosts);
            //Setting up controller in Mock repository
            var controller = new PostController(_repository.Object, _manager);

            //Act
            var result = (ViewResult)controller.Index(1);

            //Assert
            CollectionAssert.AllItemsAreInstancesOfType((ICollection)result.ViewData.Model, typeof(Post));
            Assert.IsNotNull(result, "View result is null");
            var posts = result.ViewData.Model as List<Post>;
            Assert.AreEqual(2, posts.Count, "Wrong number of posts");
        }