public void Edit_GET()
        {
            var categories    = GetCatList();
            var subCategories = GetSubCatList();

            var queue = new Queue <IEnumerable <Category> >();

            queue.Enqueue(categories);
            queue.Enqueue(subCategories);

            mockAdvertService.Setup(m => m.Find(It.IsAny <long>(), It.IsAny <Expression <Func <Advert, object> >[]>()))
            .Returns(mapper.Map <Advert>(GetAdvert()));
            mockCatService.Setup(m => m.FindAll(It.IsAny <Expression <Func <Category, bool> > >(),
                                                It.IsAny <Expression <Func <Category, object> >[]>()))
            .Returns(queue.Dequeue);
            mockCatService.Setup(m => m.FindAll(It.IsAny <Expression <Func <Category, bool> > >(),
                                                It.IsAny <Expression <Func <Category, object> >[]>()))
            .Returns(queue.Dequeue);

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


            var result = controller.Edit(4L) as PartialViewResult;
            var model  = result.Model as AdvertViewModel;
            var subs   = result.ViewData["SubCategories"] as IEnumerable <SelectListItem>;
            var cats   = result.ViewData["Categories"] as IEnumerable <SelectListItem>;

            Assert.Equal(2, model.ParentID);
            Assert.Equal(6, model.CategoryID);
            Assert.Equal(2, subs.Count());
            Assert.Equal(3, cats.Count());
        }
        public void EditValidModelState_POST_Else()
        {
            mockAdvertService.Setup(m => m.Update(It.IsAny <Advert>(), It.IsAny <Object[]>(),
                                                  It.IsAny <string[]>())).Returns(0);
            mockAdvertService.Setup(m => m.RemoveAllPictures(It.IsAny <long>()))
            .Returns(2);

            var model = GetAdvert();

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

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var result = controller.Edit(model) as JsonResult;
            var status = controller.HttpContext.Response.StatusCode;

            Assert.Equal(201, status);
            Assert.Equal("Process completed but no rows were affected", result.Value);
        }
        public void EditInvalidModelState_POST()
        {
            var categories    = GetCatList();
            var subCategories = GetSubCatList();

            var queue = new Queue <IEnumerable <Category> >();

            queue.Enqueue(categories);
            queue.Enqueue(subCategories);

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

            var model = GetAdvert();

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

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            //create ModelState.IsValid = false
            controller.ModelState.AddModelError("Location", "Location is required");

            var result      = controller.Edit(model) as PartialViewResult;
            var resultModel = result.Model as AdvertViewModel;
            var subs        = result.ViewData["SubCategories"] as IEnumerable <SelectListItem>;
            var cats        = result.ViewData["Categories"] as IEnumerable <SelectListItem>;
            var status      = controller.HttpContext.Response.StatusCode;

            Assert.Equal(6, resultModel.CategoryID);
            Assert.Equal(2, subs.Count());
            Assert.Equal(3, cats.Count());
            Assert.Equal(200, status);
        }