Exemple #1
0
        public void Post_Create_Model_Not_Valid()
        {
            // Arrange
            var menuItemViewModel = new MenuItemViewModel()
            {
                Position = 1,
                Title    = "Strona główna",
                //ActionName = "Index",
                //ControllerName = "Home"
            };
            var validator = new ModelValidator <MenuItemViewModel>(menuItemViewModel);
            var service   = Substitute.For <IMenuItemRepository>();
            var mapper    = Substitute.For <IMapper>();

            var controller = new MenuItemController(service, mapper);

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

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

            // Assert
            Assert.That(valid, Is.False);
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(actionName));
            Assert.That("Wystąpił błąd w formularzu, spróbuj ponownie.", Is.EqualTo(tempData));
        }
Exemple #2
0
        public void Good_Post_Create()
        {
            // Arrange
            var menuItemViewModel = new MenuItemViewModel()
            {
                Position       = 1,
                Title          = "Strona glowna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var menuItem = new MenuItem()
            {
                Position       = 1,
                Title          = "Strona glowna",
                ActionName     = "Index",
                ControllerName = "Home"
            };
            var validator  = new ModelValidator <MenuItemViewModel>(menuItemViewModel);
            var service    = Substitute.For <IMenuItemRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new MenuItemController(service, mapper);

            mapper.Map <MenuItemViewModel, MenuItem>(menuItemViewModel).Returns(menuItem);
            service.Insert(menuItem);
            service.Save();

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

            validator.AddToModelError(controller);
            var result     = controller.Create(menuItemViewModel) as RedirectToRouteResult;
            var actionName = result.RouteValues.Values.ElementAt(0);

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