Exemplo n.º 1
0
        public void _Returns_View_WithModel(
            string cloud, string apiKey, string apiSecret, int postId)
        {
            //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.Details(postId) as ViewResult;

            //Assert
            mockedPostService.Verify(s => s.GetPostById(postId), Times.Once);
        }
Exemplo n.º 2
0
        public void _Returns_Correct_Model(
            string cloud, string apiKey, string apiSecret, int postId)
        {
            //Arrange
            var post  = new Models.Post();
            var model = new PostDetailsViewModel();

            var mockedAuthProvider = new Mock <IAuthenticationProvider>();

            var mockedPostService = new Mock <IPostService>();

            mockedPostService.Setup(s => s.GetPostById(It.IsAny <int>())).Returns(post);

            var mockedViewModelFactory = new Mock <IViewModelFactory>();

            mockedViewModelFactory.Setup(v => v.CreatePostDetailsViewModel(It.IsAny <Models.Post>())).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.Details(postId) as ViewResult;

            //Assert
            Assert.AreEqual(model, res.Model);
        }
Exemplo n.º 3
0
        public void DetailsShould_ReturnCorrectModelView()
        {
            var mockPost = new Post()
            {
                Id          = Guid.NewGuid(),
                Title       = "testTitle",
                ImageUrl    = "testUrl",
                ImageTarget = "testTarget",
                ImageInfo   = "testInfo",
                Location    = "TestLocation",
                Time        = DateTime.Now,
                AuthorId    = "authorId",
                Author      = new Data.Models.Users.User()
                {
                    UserName = "******",
                    Id       = "testUserId"
                }
            };

            var mockPostService = new Mock <IPostService>();
            var mockUserService = new Mock <IUserService>();

            mockPostService.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(mockPost);


            var sut    = new PostController(mockPostService.Object, mockUserService.Object);
            var result = sut.Details(Guid.NewGuid()) as ViewResult;
            var model  = result.Model as PostViewModel;

            Assert.AreEqual(mockPost.Title, model.Title);
        }
Exemplo n.º 4
0
        public async Task Details_ModelIsValid_AsksForDetailsView()
        {
            Mock <PostService> mock       = new Mock <PostService>();
            PostController     controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Details(1)) as ViewResult;

            Assert.AreEqual("Details", result.ViewName);
        }
Exemplo n.º 5
0
        public void CanReturnProperPost()
        {
            //Arrange
            PostController target = new PostController(_postRepositoryMock.Object);

            //Act
            Post result = (Post)target.Details(1).Model;

            //Assert
            Assert.AreEqual(result.Id, 1);
        }
Exemplo n.º 6
0
        public async Task Details_ModelIsInvalid_AsksForErrorView()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Details(1)) as ViewResult;

            Assert.AreEqual("Error", result.ViewName);
        }
Exemplo n.º 7
0
        public void DetailsShould_ReturnHttpNotFound_WhenNoMathingPostWereFound()
        {
            var mockPostService = new Mock <IPostService>();
            var mockUserService = new Mock <IUserService>();

            mockPostService.Setup(x => x.GetById(It.IsAny <Guid>())).Returns((Post)null);


            var sut = new PostController(mockPostService.Object, mockUserService.Object);

            Assert.IsInstanceOf <HttpNotFoundResult>(sut.Details(Guid.NewGuid()));
        }
Exemplo n.º 8
0
        public async Task Details_ModelIsInvalid_RetrievesExceptionMessageFromModel()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Details(1)) as ViewResult;

            string[] model = result.ViewData.Model as string[];
            Assert.AreEqual("FindByIdAsync method throws Exception", model[0]);
        }
Exemplo n.º 9
0
        public void ThrowArgumentNullException_WhenPostServiceReturnsNull()
        {
            // Arrange
            Post post             = null;
            var  postsServiceMock = new Mock <IPostService>();

            postsServiceMock.Setup(m => m.GetById(It.IsAny <Guid>()))
            .Returns(post);
            PostController postController = new PostController(postsServiceMock.Object);

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => postController.Details(It.IsAny <Guid>()));
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public async Task Details_ModelIsValid_RetrievesPostFromModel()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).ReturnsAsync((int?_id) => new PostDTO {
                Id    = _id.Value,
                Title = "Programmer"
            });
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Details(2)) as ViewResult;

            PostViewModel model = result.ViewData.Model as PostViewModel;

            Assert.AreEqual(2, model.Id);
            Assert.AreEqual("Programmer", model.Title);
        }
Exemplo n.º 12
0
        public void Details_Passes_A_Hero_To_View()
        {
            var result = underTest.Details(1);

            Assert.IsType <ViewResult>(result);
        }
Exemplo n.º 13
0
 public void Details_PostDontExists_ShouldReturn404()
 {
     var result = _sut.Details(0);
 }