Example #1
0
        public void Post_Edit_Model_Is_Null()
        {
            // Arrange
            var      id                = -1;
            MenuItem menuItem          = null;
            var      menuItemViewModel = new MenuItemViewModel()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona glowna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var service    = Substitute.For <IMenuItemRepository>();
            var mapper     = Substitute.For <IMapper>();
            var validator  = new ModelValidator <MenuItemViewModel>(menuItemViewModel);
            var controller = new MenuItemController(service, mapper);

            service.Get(id).Returns(menuItem);

            // Act
            var valid = validator.IsValid();

            validator.AddToModelError(controller);
            var result     = controller.Edit(menuItemViewModel) as HttpNotFoundResult;
            var statusCode = result.StatusCode;

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(valid, Is.True);
            Assert.That(404, Is.EqualTo(statusCode));
        }
Example #2
0
        public void Post_Edit_Is_Not_Valid()
        {
            // Arrange
            var menuItemViewModel = new MenuItemViewModel()
            {
                ID = 1,
                //Position = 1,
                Title = "Strona główna",
                //ActionName = "Index",
                ControllerName = "Home"
            };
            var service    = Substitute.For <IMenuItemRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new MenuItemController(service, mapper);
            var validator  = new ModelValidator <MenuItemViewModel>(menuItemViewModel);

            // Act
            var valid = validator.IsValid();

            validator.AddToModelError(controller);
            var result     = controller.Edit(menuItemViewModel) as RedirectToRouteResult;
            var actionName = result.RouteValues.Values.ElementAt(0);
            var tempData   = controller.TempData["ModelIsNotValid"];

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(valid, Is.False);
            Assert.That("Index", Is.EqualTo(actionName));
        }
Example #3
0
        public void Good_Post_Edit()
        {
            // Arrange
            var menuItem = new MenuItem()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona glowna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var menuItemViewModel = new MenuItemViewModel()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona glowna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var service        = Substitute.For <IMenuItemRepository>();
            var mapper         = Substitute.For <IMapper>();
            var validator      = new ModelValidator <MenuItemViewModel>(menuItemViewModel);
            var fakeController = new FakeController();
            var controller     = new MenuItemController(service, mapper);

            fakeController.PrepareFakeAjaxRequest();
            controller.ControllerContext = fakeController.GetControllerContext <MenuItemController>(new RouteData(), controller);
            service.Get(menuItem.ID).Returns(menuItem);
            mapper.Map <MenuItemViewModel, MenuItem>(menuItemViewModel).Returns(menuItem);
            service.Update(menuItem);
            service.Save();

            // Act
            var valid = validator.IsValid();

            validator.AddToModelError(controller);
            var result      = controller.Edit(menuItemViewModel) as RedirectToRouteResult;
            var actionName  = result.RouteValues.Values.ElementAt(0);
            var ajaxRequest = controller.Request.IsAjaxRequest();

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(actionName));
            Assert.That(ajaxRequest, Is.True);
            Assert.That(valid, Is.True);
        }
Example #4
0
        public void Get_Edit_Id_Is_Null()
        {
            // Arrange
            int?id             = null;
            var service        = Substitute.For <IMenuItemRepository>();
            var mapper         = Substitute.For <IMapper>();
            var fakeController = new FakeController();
            var controller     = new MenuItemController(service, mapper);

            // Act
            var result     = controller.Edit(id) as HttpStatusCodeResult;
            var statusCode = result.StatusCode;

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(400, Is.EqualTo(statusCode));
        }
Example #5
0
        public void Get_Edit_Model_Is_Null()
        {
            // Arrange
            var      id         = 1;
            MenuItem menuItem   = null;
            var      service    = Substitute.For <IMenuItemRepository>();
            var      mapper     = Substitute.For <IMapper>();
            var      controller = new MenuItemController(service, mapper);

            service.Get(id).Returns(menuItem);

            // Act
            var result     = controller.Edit(id) as HttpNotFoundResult;
            var statusCode = result.StatusCode;

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(404, Is.EqualTo(statusCode));
        }
Example #6
0
        public void Good_Get_Edit()
        {
            // Arrange
            var id       = 1;
            var menuItem = new MenuItem()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona główna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var menuItemViewModel = new MenuItemViewModel()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona główna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var service        = Substitute.For <IMenuItemRepository>();
            var mapper         = Substitute.For <IMapper>();
            var fakeController = new FakeController();
            var controller     = new MenuItemController(service, mapper);

            fakeController.PrepareFakeAjaxRequest();
            controller.ControllerContext = fakeController.GetControllerContext <MenuItemController>(new RouteData(), controller);
            service.Get(id).Returns(menuItem);
            mapper.Map <MenuItem, MenuItemViewModel>(menuItem).Returns(menuItemViewModel);

            // Act
            var result      = controller.Edit(id) as PartialViewResult;
            var viewName    = result.ViewName;
            var model       = result.Model;
            var ajaxRequest = controller.Request.IsAjaxRequest();

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("_EditPartial", Is.EqualTo(viewName));
            Assert.That(ajaxRequest, Is.True);
            Assert.That(model, !Is.Null);
        }
Example #7
0
        public void Get_Edit_Not_Ajax_Request()
        {
            // Arrange
            var id       = 1;
            var menuItem = new MenuItem()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona główna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var menuItemViewModel = new MenuItemViewModel()
            {
                ID             = 1,
                Position       = 1,
                Title          = "Strona główna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var service        = Substitute.For <IMenuItemRepository>();
            var mapper         = Substitute.For <IMapper>();
            var fakeController = new FakeController();
            var controller     = new MenuItemController(service, mapper);

            fakeController.PrepareFakeRequest();
            controller.ControllerContext = fakeController.GetControllerContext <MenuItemController>(new RouteData(), controller);
            service.Get(id).Returns(menuItem);
            mapper.Map <MenuItem, MenuItemViewModel>(menuItem).Returns(menuItemViewModel);

            // Act
            var result      = controller.Edit(id) as RedirectToRouteResult;
            var actionName  = result.RouteValues.Values.ElementAt(0);
            var ajaxRequest = controller.Request.IsAjaxRequest();

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(actionName));
            Assert.That(ajaxRequest, Is.False);
        }