Esempio n. 1
0
        public void CreateArticleGetView()
        {
            // Dictionary mock
            var allArticleTypes = new List <ArticleType> {
                new ArticleType {
                    Code = "ASP"
                },
                new ArticleType {
                    Code = "WCF"
                },
                new ArticleType {
                    Code = "Main", IsMain = true
                }
            };

            _dictionarySrvMock.Setup(m => m.GetDictionary <ArticleType>()).Returns(allArticleTypes);

            // AutoMapper mock
            var expectedMappedArticleTypes = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "ASP"
                },
                new SelectListItem()
                {
                    Text = "WCF"
                }
            };

            _mapperMock
            .Setup(m =>
                   m.Map <IEnumerable <SelectListItem> >
                       (It.IsAny <IEnumerable <ArticleType> >()))
            .Returns(expectedMappedArticleTypes);

            ViewResult actionResult = _articleController.CreateArticle();

            // type of model in ViewResult
            Assert.IsNotNull(actionResult);

            object model = actionResult.Model;

            Assert.IsInstanceOf <ArticleModel>(model);

            var properArticleTypes = _dictionarySrvMock.Object.GetDictionary <ArticleType>()
                                     .Where(x => x.IsMain == false);

            // was mock invoked
            _mapperMock.Verify(x => x.Map <IEnumerable <SelectListItem> >(properArticleTypes), Times.Once);

            // article types count
            Assert.AreEqual(properArticleTypes.Count(), ((ArticleModel)model).ArticleTypes.Count(),
                            "Incorrect article types count");
        }
Esempio n. 2
0
        public void TestCreateArticle()
        {
            var controller = new ArticleController(mockArticleService.Object, mockPostService.Object);


            var contextMock = new Mock <HttpContext>();

            var claims = new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "abc"),
                new Claim(ClaimTypes.Role, "member"),
                new Claim("user_id", "5d247a04eff1030d7c520v321")
            });

            contextMock.Setup(x => x.User).Returns(new ClaimsPrincipal(claims));
            mockPostService.Setup(x => x.Add(It.IsAny <Post>())).Returns(article.Post);
            mockArticleService.Setup(x => x.Add(It.IsAny <Article>())).Returns(article);

            controller.ControllerContext.HttpContext = contextMock.Object;

            IActionResult  createArticle  = controller.CreateArticle(article);
            OkObjectResult okObjectResult = createArticle as OkObjectResult;
            Article        articleActual  = (Article)okObjectResult.Value;

            Assert.AreEqual(articleActual, article);
        }
Esempio n. 3
0
        public void Test_ArticleController_CreateArticle_GET()
        {
            // Arrange
            utils.CleanTables();
            Tuple <string, string> userIds = utils.CreateUsers();
            int article1 = utils.CreateSingleArticle(userIds.Item2);
            int article2 = utils.CreateSingleArticle(userIds.Item2);
            ArticleController controller = ControllerSetup(userIds.Item2, RoleType.Employee.ToString());

            // Act
            string viewName = controller.CreateArticle().ViewName;

            // Assert
            Assert.AreEqual <string>("CreateArticle", viewName);
        }
Esempio n. 4
0
        public void Test_ArticleController_CreateArticleTest_POST_No_Publisher()
        {
            // Arrange
            utils.CleanTables();
            Tuple <string, string> userIds = utils.CreateUsers();
            int article1 = utils.CreateSingleArticle(userIds.Item2);
            int article2 = utils.CreateSingleArticle(userIds.Item2);
            ArticleController controller   = ControllerSetup(userIds.Item1, RoleType.Employee.ToString());
            string            edited_title = "created_title";
            string            edited_body  = "created_body";
            ArticleViewModel  vm           = new ArticleViewModel()
            {
                Title = edited_title, Body = edited_body
            };

            // Act
            string viewName = (controller.CreateArticle(vm) as ViewResult).ViewName;

            // Assert
            Assert.AreEqual <string>("Error", viewName);
        }
Esempio n. 5
0
        public void Test_ArticleController_CreateArticle_POST()
        {
            // Arrange
            utils.CleanTables();
            Tuple <string, string> userIds = utils.CreateUsers();
            int article1 = utils.CreateSingleArticle(userIds.Item2);
            int article2 = utils.CreateSingleArticle(userIds.Item2);
            ArticleController controller   = ControllerSetup(userIds.Item2, RoleType.Employee.ToString());
            string            edited_title = "edited_title";
            string            edited_body  = "edited_body";
            ArticleViewModel  vm           = new ArticleViewModel()
            {
                Title = edited_title, Body = edited_body
            };

            // Act
            string url = (controller.CreateArticle(vm) as RedirectResult).Url;

            // Assert
            Assert.AreEqual <string>("ArticleList", url);
        }