public void Create_InvalidModelState_POST()
        {
            var             categories    = GetCatList();
            var             subcategories = GetSubCatList();
            AdvertViewModel model         = new AdvertViewModel
            {
                CategoryID = 2,
                ParentID   = 1
            };

            Expression <Func <Category, bool> > where = c => c.ParentID == null;
            Expression <Func <Category, bool> > subwhere = x => x.ParentID == 1;



            var controller = new ClassifiedsController(mockAdvertService.Object,
                                                       mockCatService.Object, mapper);

            controller.ModelState.AddModelError("category", "please select category");//set ModelState to be invalid

            var result = controller.Create(model) as ViewResult;
            var list   = result.ViewData["Categories"] as IEnumerable <SelectListItem>;
            var subs   = result.ViewData["SubCategories"] as IEnumerable <SelectListItem>;

            Assert.Equal(3, list.Count());
            Assert.Equal(2, subs.Count());
            Assert.True(subs.FirstOrDefault(x => x.Value.Equals("2")).Selected);
            Assert.True(list.FirstOrDefault(x => x.Value.Equals("1")).Selected);
        }
        public void Create_GET()
        {
            IEnumerable <Category> categories = GetCatList();

            //Setup to allow query of authenticated user
            //  (HttpContext.User.Claims.FirstOrDefault(u => u.Type == "UserId").Value)
            var mockIdentity = new GenericIdentity("User");

            mockIdentity.AddClaim(new Claim("UserId", "1"));
            var principal = new GenericPrincipal(mockIdentity, null);


            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.Setup(m => m.User).Returns(principal);

            mockCatService.Setup(m => m.FindAll(It.IsAny <Expression <Func <Category, bool> > >(),
                                                It.IsAny <Expression <Func <Category, object> >[]>()))
            .Returns(categories);

            var controller = new ClassifiedsController(mockAdvertService.Object,
                                                       mockCatService.Object, mapper);

            controller.ControllerContext.HttpContext = mockHttpContext.Object;

            var result        = controller.Create() as ViewResult;
            var categoryList  = result.ViewData["Categories"] as IEnumerable <SelectListItem>;
            var subCategories = result.ViewData["SubCategories"] as IEnumerable <SelectListItem>;


            Assert.IsType <AdvertViewModel>(result.Model);
            Assert.Equal(3, categoryList.Count());
            Assert.Empty(subCategories);
        }
        public void CreateValidModelState_POST()
        {
            AdvertViewModel model = GetAdvert();

            var controller = new ClassifiedsController(mockAdvertService.Object,
                                                       mockCatService.Object, mapper);
            var result = controller.Create(model) as RedirectToActionResult;

            Assert.Equal("Index", result.ActionName);
            Assert.Equal("Classifieds", result.ControllerName);
            mockAdvertService.Verify(m => m.Create(It.IsAny <Advert>()), Times.Once());
            mockAdvertService.Verify(m => m.Save(), Times.Once());
        }