Exemplo n.º 1
0
        public void Post_Edit_Is_Not_Valid()
        {
            // Arrange
            var newsViewModel = new NewsViewModel()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                //Position = 1,
                Title = "New 1",
                //Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var service    = Substitute.For <INewsRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new NewsController(service, mapper);
            var validator  = new ModelValidator <NewsViewModel>(newsViewModel);

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

            validator.AddToModelError(controller);
            var result     = controller.Edit(newsViewModel) 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));
        }
Exemplo n.º 2
0
        public void Post_Edit_News_Is_Null()
        {
            // Arrange
            var  id            = -1;
            News news          = null;
            var  newsViewModel = new NewsViewModel()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var service    = Substitute.For <INewsRepository>();
            var mapper     = Substitute.For <IMapper>();
            var validator  = new ModelValidator <NewsViewModel>(newsViewModel);
            var controller = new NewsController(service, mapper);

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

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

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

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(valid, Is.True);
            Assert.That(404, Is.EqualTo(statusCode));
        }
Exemplo n.º 3
0
        public void Good_Post_Edit()
        {
            // Arrange
            var news = new News()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var resultNews = new News()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New ",
                Content   = "Quisque , tempor eu lorem non, pharetra laoreet massa."
            };
            var newsViewModel = new NewsViewModel()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New ",
                Content   = "Quisque , tempor eu lorem non, pharetra laoreet massa."
            };
            var service        = Substitute.For <INewsRepository>();
            var mapper         = Substitute.For <IMapper>();
            var validator      = new ModelValidator <NewsViewModel>(newsViewModel);
            var fakeController = new FakeController();
            var controller     = new NewsController(service, mapper);

            fakeController.PrepareFakeAjaxRequest();
            controller.ControllerContext = fakeController.GetControllerContext <NewsController>(new RouteData(), controller);
            service.Get(news.ID).Returns(news);
            mapper.Map <NewsViewModel, News>(newsViewModel, news).Returns(resultNews);
            service.Update(news);
            service.Save();

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

            validator.AddToModelError(controller);
            var result      = controller.Edit(newsViewModel) 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);
        }
        public void GetNewsReturnsNotFoundResultWhenNewsNotFound()
        {
            // Arrange
            int testNewsId = 1;
            var mock       = new Mock <IRepository <News> >();

            mock.Setup(repo => repo.GetById(testNewsId))
            .Returns(null as News);
            var controller = new NewsController(mock.Object);

            // Act
            var result = controller.Edit(testNewsId);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 5
0
        public void Get_Edit_Id_Is_Null()
        {
            // Arrange
            int?id         = null;
            var service    = Substitute.For <INewsRepository>();
            var mapper     = Substitute.For <IMapper>();
            var controller = new NewsController(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));
        }
Exemplo n.º 6
0
        public void Get_Edit_Model_Is_Null()
        {
            // Arrange
            var  id         = 1;
            News news       = null;
            var  service    = Substitute.For <INewsRepository>();
            var  mapper     = Substitute.For <IMapper>();
            var  controller = new NewsController(service, mapper);

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

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

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(404, Is.EqualTo(statusCode));
        }
Exemplo n.º 7
0
        public void Good_Get_Edit()
        {
            // Arrange
            var id   = 1;
            var news = new News()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New 1",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var newsViewModel = new NewsViewModel()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New 1",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var service        = Substitute.For <INewsRepository>();
            var mapper         = Substitute.For <IMapper>();
            var fakeController = new FakeController();
            var controller     = new NewsController(service, mapper);

            fakeController.PrepareFakeAjaxRequest();
            controller.ControllerContext = fakeController.GetControllerContext <NewsController>(new RouteData(), controller);
            service.Get(id).Returns(news);
            mapper.Map <News, NewsViewModel>(news).Returns(newsViewModel);

            // 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);
        }
        public void GetNewsReturnsViewResultWithNews()
        {
            // Arrange
            int testNewsId = 1;
            var mock       = new Mock <IRepository <News> >();

            mock.Setup(repo => repo.GetById(testNewsId))
            .Returns(GetTestNews().FirstOrDefault(p => p.Id == testNewsId));
            var controller = new NewsController(mock.Object);

            // Act
            var result = controller.Edit(testNewsId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <News>(viewResult.ViewData.Model);

            Assert.Equal("First title", model.Title);
            Assert.Equal("some text", model.Content);
        }
Exemplo n.º 9
0
        public void Get_Edit_Not_Ajax_Request()
        {
            // Arrange
            var id   = 1;
            var news = new News()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New 1",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var newsViewModel = new NewsViewModel()
            {
                ID        = 1,
                AddedDate = DateTime.Now,
                Position  = 1,
                Title     = "New 1",
                Content   = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa."
            };
            var service        = Substitute.For <INewsRepository>();
            var mapper         = Substitute.For <IMapper>();
            var fakeController = new FakeController();
            var controller     = new NewsController(service, mapper);

            fakeController.PrepareFakeRequest();
            controller.ControllerContext = fakeController.GetControllerContext <NewsController>(new RouteData(), controller);
            service.Get(id).Returns(news);
            mapper.Map <News, NewsViewModel>(news).Returns(newsViewModel);

            // 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);
        }
Exemplo n.º 10
0
        public async Task ValidModelStateOnGetBeforeUpdate()
        {
            // Assign
            var id              = 1;
            var news            = GetFakeNews();
            var dateTimeFactory = new DateTimeFactory();
            var fileServiceMock = new Mock <IFileStorageService>();

            DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true);

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object);

                // Act
                var response = await newsController.Edit(id);

                var newsContent = response.GetModelAs <NewsViewModel>();

                // Assert
                Assert.IsType <ViewResult>(response);
                Assert.Equal(id, newsContent.Id);
            }
        }