Exemple #1
0
        public void Post_Edit_Not_Valid()
        {
            // Arrange
            var componentViewModel = new ComponentViewModel()
            {
                Name = "Sałata 112"
            };
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new ComponentController(repository, mapper);
            var validator  = new ModelValidator <ComponentViewModel>(componentViewModel);

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

            validator.AddToModelError(controller);
            var result   = controller.Edit(componentViewModel) as ViewResult;
            var viewName = result.ViewName;
            var tempData = controller.TempData["ModelIsNotValid"];

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(valid, Is.False);
            Assert.That("Index", Is.EqualTo(viewName));
            Assert.That("Wystąpił błąd w formularzu, spróbuj ponownie.", Is.EqualTo(tempData));
        }
Exemple #2
0
        public void Post_Edit_Component_Is_Null()
        {
            // Arrange
            Component component          = null;
            var       componentViewModel = new ComponentViewModel()
            {
                Name = "Sałata"
            };
            var id         = componentViewModel.ID;
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();
            var validator  = new ModelValidator <ComponentViewModel>(componentViewModel);
            var controller = new ComponentController(repository, mapper);

            repository.Get(id).Returns(component);

            // Act
            var result     = controller.Edit(componentViewModel) as HttpNotFoundResult;
            var statusCode = result.StatusCode;
            var valid      = validator.IsValid();

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(404, Is.EqualTo(statusCode));
            Assert.That(valid, Is.True);
        }
Exemple #3
0
        public void Good_Post_Edit()
        {
            // Arrange
            var component = new Component()
            {
                Name = "Sałata"
            };
            var componentViewModel = new ComponentViewModel()
            {
                Name = "Sałata"
            };
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new ComponentController(repository, mapper);
            var validator  = new ModelValidator <ComponentViewModel>(componentViewModel);

            repository.Get(componentViewModel.ID).Returns(component);
            mapper.Map <ComponentViewModel, Component>(componentViewModel).Returns(component);
            repository.Update(component);
            repository.Save();

            // Act
            var result     = controller.Edit(componentViewModel) as RedirectToRouteResult;
            var actionName = result.RouteValues.Values.ElementAt(0);
            var valid      = validator.IsValid();

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(actionName));
            Assert.That(valid, Is.True);
        }
Exemple #4
0
        public void Get_Edit_Not_Ajax_Request()
        {
            // Arrange
            var component = new Component()
            {
                Name = "Sałata"
            };
            var componentViewModel = new ComponentViewModel()
            {
                Name = "Sałata"
            };
            var id         = 1;
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();

            var fakeController = new FakeController();

            fakeController.PrepareFakeRequest();
            var controller = new ComponentController(repository, mapper);

            controller.ControllerContext = fakeController.GetControllerContext <ComponentController>(new RouteData(), controller);

            repository.Get(id).Returns(component);
            mapper.Map <Component, ComponentViewModel>(component).Returns(componentViewModel);

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

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(ajaxRequest, Is.False);
            Assert.That("Index", Is.EqualTo(actionName));
        }
Exemple #5
0
        public void Get_Edit_Id_Is_Null()
        {
            // Arrange
            int?id         = null;
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new ComponentController(repository, 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));
        }
Exemple #6
0
        public void Good_Get_Edit()
        {
            // Arrange
            var component = new Component()
            {
                Name = "Sałata"
            };
            var componentViewModel = new ComponentViewModel()
            {
                Name = "Sałata"
            };
            var id         = 1;
            var repository = Substitute.For <IComponentRepository>();
            var mapper     = Substitute.For <IMapper>();

            var fakeController = new FakeController();

            fakeController.PrepareFakeAjaxRequest();
            var controller = new ComponentController(repository, mapper);

            controller.ControllerContext = fakeController.GetControllerContext <ComponentController>(new RouteData(), controller);

            repository.Get(id).Returns(component);
            mapper.Map <Component, ComponentViewModel>(component).Returns(componentViewModel);

            // 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(model, !Is.Null);
            Assert.That(ajaxRequest, Is.True);
        }