Exemplo n.º 1
0
        public void EditPost_WithFile()
        {
            UserPost editPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            Mock <HttpContextBase>    contextMock = new Mock <HttpContextBase>();
            Mock <HttpPostedFileBase> file        = new Mock <HttpPostedFileBase>();

            // The required properties from my Controller side
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello"));

            file.Setup(x => x.InputStream).Returns(stream);
            file.Setup(x => x.ContentLength).Returns((int)stream.Length);
            file.Setup(x => x.FileName).Returns("Test.js");
            string basePath = @"c:\yourPath\App";

            contextMock.Setup(x => x.Server.MapPath(It.IsAny <String>())).Returns(basePath);
            file.Setup(x => x.SaveAs(It.IsAny <String>())).Verifiable();


            string folderPath     = basePath + HardcodedValues.UserFiles + _loginUser.UserId;
            var    fakeFileSystem =
                new MockFileSystem(new Dictionary <string, MockFileData> {
                { folderPath, new MockDirectoryData() }
            });

            _postsRepoMock.Setup(m => m.EditPost(editPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser, fakeFileSystem);

            RequestContext rc = new RequestContext(contextMock.Object, new RouteData());

            controller.ControllerContext = new ControllerContext(rc, controller);

            JsonResult result = controller.EditPost(editPost, file.Object);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "OK");
            Assert.IsTrue(!String.IsNullOrEmpty((result.Data as JsonResponseVM).Msg));
        }
Exemplo n.º 2
0
        public void EditPost_DatabaseSaveFailed()
        {
            UserPost editPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            _postsRepoMock.Setup(m => m.EditPost(editPost)).Returns(false);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.EditPost(editPost);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "ERROR");
            Assert.IsTrue(!String.IsNullOrEmpty((result.Data as JsonResponseVM).Msg));
        }
Exemplo n.º 3
0
        public void EditPost()
        {
            UserPost editPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            _postsRepoMock.Setup(m => m.EditPost(editPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.EditPost(editPost);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "OK");
            Assert.IsTrue((result.Data as JsonResponseVM).PostId == editPost.PostId);
        }
Exemplo n.º 4
0
        public void EditPost_ModelStateNotValid()
        {
            UserPost editPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            _postsRepoMock.Setup(m => m.EditPost(editPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            controller.ModelState.AddModelError("UnitTest", "TestError");

            JsonResult result = controller.EditPost(editPost);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "ERROR");
            Assert.IsTrue((result.Data as JsonResponseVM).Msg == "TestError");
        }