public PersonController(IRepository<Person> personRepository, IRepository<Section> sectionRepository)
 {
     this.personRepository = personRepository;
     this.sectionRepository = sectionRepository;
     section_ctrl = new SectionController(sectionRepository);
     model = new PersonListView(personRepository, section_ctrl);
 }
        public void CanDeleteSection()
        {
            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            DeleteObjectByName model = new DeleteObjectByName() { Name = "IMZ4" };

            var redirectToRouteResult = controller.Delete(model) as RedirectToRouteResult;
            mock_section.Verify(m => m.DeleteObject(It.IsAny<Section>()), Times.Once());

            Assert.IsNotNull(redirectToRouteResult);
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]);
        }
        public void CanEditSection()
        {
            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            SectionModel section = new SectionModel();

            Section temp = mock_section.Object.Repository.FirstOrDefault(x => x.short_name == "IMZ6");
            Assert.IsNotNull(temp);
            Assert.AreEqual(temp.id, 6);
            Assert.AreEqual(temp.name, "FFF");
            Assert.AreEqual(temp.email, "*****@*****.**");
            Assert.AreEqual(temp.locality, "Łódź");
            Assert.AreEqual(temp.phone_number, "723178153");
            Assert.AreEqual(temp.postal_code, "22-336");
            Assert.AreEqual(temp.post, "Łódź");
            Assert.AreEqual(temp.short_name, "IMZ6");
            Assert.AreEqual(temp.street, "Spółdzielców 61");

            section.id = 6;
            section.email = "*****@*****.**";
            section.locality = "Radom";
            section.name = "YYY";
            section.phone_number = "551111111";
            section.post = "Radom";
            section.postal_code = "11-111";
            section.short_name = "IMZ111";
            section.street = "Łódzka 55";

            var redirectToRouteResult = controller.Edit(section) as RedirectToRouteResult;

            temp = mock_section.Object.Repository.FirstOrDefault(x => x.id == 6);
            Assert.AreEqual(temp.name, "YYY");
            Assert.AreEqual(temp.email, "*****@*****.**");
            Assert.AreEqual(temp.locality, "Radom");
            Assert.AreEqual(temp.phone_number, "551111111");
            Assert.AreEqual(temp.postal_code, "11-111");
            Assert.AreEqual(temp.post, "Radom");
            Assert.AreEqual(temp.short_name, "IMZ111");
            Assert.AreEqual(temp.street, "Łódzka 55");

            mock_section.Verify(m => m.EditObject(temp), Times.Once());

            Assert.IsNotNull(redirectToRouteResult);
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]);
        }
        public void CanAddSection()
        {
            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            SectionModel section = new SectionModel();

            section.email = "*****@*****.**";
            section.locality = "Gdańsk";
            section.name = "XXX";
            section.phone_number = "552134152";
            section.post = "Gdańsk";
            section.postal_code = "22-242";
            section.short_name = "IMZ8";
            section.street = "Królewska 11";

            var redirectToRouteResult = controller.Add(section) as RedirectToRouteResult;
            mock_section.Verify(m => m.AddObject(It.IsAny<Section>()), Times.Once());

            Assert.IsNotNull(redirectToRouteResult);
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]);
        }
        public void GetAllShortNames()
        {
            Mock<IRepository<Section>> mock = new Mock<IRepository<Section>>();
            mock.Setup(m => m.Repository).Returns(new Section []
            {
                new Section { id = 1, short_name = "IMZ1" },
                new Section { id = 2, short_name = "IMZ2" },
                new Section { id = 3, short_name = "IMZ3" },
                new Section { id = 4, short_name = "IMR1" },
                new Section { id = 5, short_name = "IMR2" },
                new Section { id = 3, short_name = "IMZ3" },
            }.AsQueryable());

            SectionController controller = new SectionController(mock.Object);

            Dictionary<int, string> lista = controller.GetAllShortNameSections();
            Assert.AreEqual(lista.Count, 5);
            Assert.AreEqual(lista[4], "IMR1");
        }
        public void GetAllSections()
        {
            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            Assert.AreEqual(mock_section.Object.Repository.Count(), 6);
        }
        public void CantEditSection()
        {
            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            SectionModel section = null;

            var redirectToRouteResult = controller.Edit(section) as RedirectToRouteResult;

            mock_section.Verify(m => m.EditObject(It.IsAny<Section>()), Times.Never());

            Assert.IsNotNull(redirectToRouteResult);
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]);
        }
        public void CantDeleteSectionIfPersonListExist()
        {
            Mock<HttpRequestBase> request = new Mock<HttpRequestBase>();
            Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();
            Mock<HttpContextBase> context = new Mock<HttpContextBase>();

            context.Setup(c => c.Request).Returns(request.Object);
            context.Setup(c => c.Response).Returns(response.Object);
            //Add XMLHttpRequest request header
            request.Setup(req => req["X-Requested-With"]).
                Returns("XMLHttpRequest");

            Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>();
            mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable());

            SectionController controller = new SectionController(mock_section.Object);
            controller.ControllerContext = new ControllerContext(
            context.Object, new RouteData(), controller);
            DeleteObjectByName model = new DeleteObjectByName() { Name = "IMZ1" };
            var viewResult = controller.Delete(model.Name) as PartialViewResult;

            Assert.IsNotNull(viewResult);
            Assert.AreEqual("Section/_SectionDelete", viewResult.ViewName);
            mock_section.Verify(m => m.DeleteObject(It.IsAny<Section>()), Times.Never());
        }