public void AddValidData()
        {
            //_postController.Session["user"] = _userRepositoryMock.Object.FindByUsername(_users[0].Username);
            Post post = new Post()
            {
                Id = 5, Text = "as", Title = "daadsdsa"
            };
            ActionResult result = _postController.Add(post, null);

            _postRepositoryMock.Verify(x => x.Save(post));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Exemple #2
0
        public async Task Add_Post_Should_Call_Service_And_Redirect()
        {
            var user = new ApplicationUser()
            {
                Id = "test"
            };

            mockUserService.Setup(x => x.GetCurrentUserAsync())
            .Returns(Task.FromResult(user));

            var controller = new PostController(mockPostService.Object, mockMapper.Object,
                                                mockImageService.Object, mockUserService.Object);

            var postRequest = new PostRequest()
            {
                Title = "Test"
            };

            var result = await controller.Add(postRequest);

            var redirectResult = Assert.IsType <RedirectToActionResult>(result);

            mockPostService.Verify(s => s.SaveAsync(postRequest), Times.Once);
            Assert.Equal("Index", redirectResult.ActionName);
        }
        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;
        }
Exemple #4
0
        public void Add_ModelIsVAlid_Returns_RedirectToAction()
        {
            //Arrange
            var inputPostContent = "123";
            var inputId          = "1";
            var expected         = "Index/1";

            var postDTO = new PostDTO()
            {
                Content    = "abcd",
                PostDate   = DateTime.Now,
                UserId     = "12",
                UserPageId = inputId
            };

            var post = new Post()
            {
                Content         = "abcd",
                PostDate        = DateTime.Now,
                UserPageId      = inputId,
                ApplicationUser = new ApplicationUser()
                {
                    UserProfile = new UserProfile()
                }
            };

            var mockUOW     = new Mock <IUnitOfWork>();
            var mockService = new Mock <IServiceCreator>();

            mockUOW.Setup(x => x.PostRepository.Add(post));
            mockService.Setup(x => x.CreatePostService()).Returns(new PostService(mockUOW.Object));

            var postController = new PostController(mockService.Object)
            {
                GetUserId = () => "12"
            };

            //Act
            var result = postController.Add(inputPostContent, inputId) as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result.RouteValues["action"]);
        }
Exemple #5
0
        public void Add_ModelIsValid_AddPost_Ok_()
        {
            //Arrange
            var inputPostContent = "123";
            var inputId          = "1";

            var postDTO = new PostDTO()
            {
                Content    = "abcd",
                PostDate   = DateTime.Now,
                UserId     = "12",
                UserPageId = inputId
            };

            var post = new Post()
            {
                Content         = "abcd",
                PostDate        = DateTime.Now,
                UserPageId      = inputId,
                ApplicationUser = new ApplicationUser()
                {
                    UserProfile = new UserProfile()
                }
            };

            var mockUOW         = new Mock <IUnitOfWork>();
            var mockService     = new Mock <IServiceCreator>();
            var mockPostService = new Mock <IPostService>();

            mockPostService.Setup(x => x.AddPost(It.IsAny <PostDTO>()));
            mockUOW.Setup(x => x.PostRepository.Add(post));
            mockService.Setup(x => x.CreatePostService()).Returns(mockPostService.Object);

            var postController = new PostController(mockService.Object)
            {
                GetUserId = () => "12"
            };

            //Act
            var result = postController.Add(inputPostContent, inputId) as RedirectToRouteResult;

            //Assert
            mockPostService.Verify(x => x.AddPost(It.IsAny <PostDTO>()), Times.AtLeastOnce());
        }
        public void AddCallPostViewModelsubmitPublish()
        {
            //Arrange
            var mockPostLogic   = new Mock <IPostsLogic>();
            var postController  = new PostController(mockPostLogic.Object);
            var fakeHttpContext = new Mock <HttpContext>();
            var mockRequest     = new Mock <HttpRequest>();
            var formMock        = new Mock <IFormCollection>();

            fakeHttpContext.Setup(a => a.Request).Returns(mockRequest.Object);
            mockRequest.Setup(req => req.Form).Returns(formMock.Object);
            formMock.Setup(a => a["Approver"]).Returns("Approver");
            formMock.Setup(a => a.Keys).Returns(new List <string> {
                "Approved"
            });

            postController.ControllerContext.HttpContext = fakeHttpContext.Object;


            var model = new PostViewModel
            {
                Title   = "Test",
                Content = "Test Content",
                Author  = "test Author",
            };

            mockPostLogic.Setup(mock => mock.AddOrEdit(new Post
            {
                Title          = model.Title,
                Content        = model.Content,
                Author         = model.Author,
                WorkflowStates = States.Publish
            }));


            //Act
            postController.Add(model);

            //Assert

            mockPostLogic.Verify(mock => mock.AddOrEdit(It.IsAny <Post>()));
            mockRequest.VerifyAll();
        }
Exemple #7
0
        public void _Call_ViewModelFactory(string cloud, string apiKey, string apiSecret)
        {
            //Arrange
            var mockedAuthProvider     = new Mock <IAuthenticationProvider>();
            var mockedPostService      = new Mock <IPostService>();
            var mockedViewModelFactory = new Mock <IViewModelFactory>();

            var mockedAcc        = new CloudinaryDotNet.Account(cloud, apiKey, apiSecret);
            var mockedCloudinary = new Mock <Cloudinary>(mockedAcc);

            var postController = new PostController(mockedAuthProvider.Object, mockedPostService.Object,
                                                    mockedViewModelFactory.Object, mockedCloudinary.Object);

            //Act
            var res = postController.Add() as ViewResult;

            //Assert
            mockedViewModelFactory.Verify(v => v.CreateAddPostViewModel(mockedCloudinary.Object), Times.Once);
        }
Exemple #8
0
        public void Add_InputDataIsNull_Throws()
        {
            //Arrange
            string inputPostContent = null;
            string inputId          = null;

            var mock = new Mock <IServiceCreator>();

            mock.Setup(x => x.CreatePostService());

            var postController = new PostController(mock.Object)
            {
                GetUserId = () => "12"
            };

            //Act

            //Assert
            Assert.ThrowsException <ArgumentNullException>(() => postController.Add(inputPostContent, inputId));
        }
Exemple #9
0
        public void Add_ModelIsNotValid_Returns_RedirectToAction()
        {
            //Arrange
            var inputPostContent = "123";
            var inputId          = "1";
            var expectedView     = "Index/1";

            var mock = new Mock <IServiceCreator>();

            mock.Setup(x => x.CreatePostService());

            var postController = new PostController(mock.Object);

            postController.ModelState.AddModelError("", "Error");

            //Act
            var result = postController.Add(inputPostContent, inputId) as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(expectedView, result.RouteValues["action"]);
        }
Exemple #10
0
        public void _Returns_View_WithModel(string cloud, string apiKey, string apiSecret)
        {
            //Arrange
            var model = new AddPostViewModel();

            var mockedAuthProvider = new Mock <IAuthenticationProvider>();
            var mockedPostService  = new Mock <IPostService>();

            var mockedViewModelFactory = new Mock <IViewModelFactory>();

            mockedViewModelFactory.Setup(v => v.CreateAddPostViewModel(It.IsAny <Cloudinary>())).Returns(model);

            var mockedAcc        = new CloudinaryDotNet.Account(cloud, apiKey, apiSecret);
            var mockedCloudinary = new Mock <Cloudinary>(mockedAcc);

            var postController = new PostController(mockedAuthProvider.Object, mockedPostService.Object,
                                                    mockedViewModelFactory.Object, mockedCloudinary.Object);

            //Act
            var res = postController.Add() as ViewResult;

            //Assert
            Assert.AreEqual(model, res.Model);
        }