diff Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 270:3bb779434ae8

PersonaController: Coverage 100%
author juanjose.montesdeocaarbos
date Wed, 26 Oct 2011 09:31:17 -0300
parents 17694f70cfb6
children
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Tue Oct 25 23:24:57 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Wed Oct 26 09:31:17 2011 -0300
@@ -21,7 +21,7 @@
             IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
         }
 
-        private void PersonaNueva(PersonaController controller)
+        private void PersonaNueva(ref PersonaController controller)
         {
             var personaNew = new PersonaNewModel
             {
@@ -35,8 +35,9 @@
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
                 controller.Nueva(personaNew);
+                controller = new PersonaController { ControllerContext = ControllerCtx };
             }
-
+            
             personaNew = new PersonaNewModel
             {
                 Blog = "http://nelopauselli.blogspot.com/",
@@ -49,6 +50,7 @@
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
                 controller.Nueva(personaNew);
+                controller = new PersonaController { ControllerContext = ControllerCtx };
             }
         }
 
@@ -57,6 +59,7 @@
         {
             NhHelperTest.CleanDb();
             SetearUsuario();
+            ControllerCtx = new ControllerContext();
         }
 
         [Test]
@@ -75,7 +78,7 @@
         public void PersonaModificarGET()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
@@ -94,8 +97,6 @@
 
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
-                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
-
                 var resultModificar = personaController.Modificar("11111111-1111-1111-1111-111111111111");
                 Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
                 Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
@@ -169,7 +170,7 @@
         public void PersonaNuevaPOST_PersonaExistente()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             var form = new FormCollection
                            {
@@ -196,13 +197,11 @@
         public void PersonaModificarPOST_RequiredFields()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             PersonaEditModel personaEdit = null;
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
-                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
-
                 var form = new FormCollection
                                {
                                    {"Id", ""},
@@ -237,7 +236,7 @@
         public void PersonaModificarPOST_FormatFields()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             PersonaEditModel personaEdit = null;
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
@@ -276,7 +275,7 @@
         public void PersonaModificarPOST_PersonaExistente()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             PersonaEditModel personaEdit = null;
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
@@ -310,7 +309,7 @@
         public void PersonaModificarPOST_Successful()
         {
             var personaController = new PersonaController { ControllerContext = ControllerCtx };
-            PersonaNueva(personaController);
+            PersonaNueva(ref personaController);
 
             PersonaEditModel personaEdit = null;
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
@@ -337,5 +336,72 @@
                 Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
             }
         }
+
+        [Test]
+        public void PersonaQuitarGET()
+        {
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+            PersonaNueva(ref personaController);
+
+            using (new RequestEmulator(NhHelper.GetSessionFactory()))
+            {
+                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
+
+                var resultQuitar = personaController.Quitar(personaRepository.GetAll().FirstOrDefault().Id.ToString());
+
+                Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultQuitar);
+                var redirectResultQuitar = (RedirectToRouteResult)resultQuitar;
+                Assert.AreEqual("Index", redirectResultQuitar.RouteValues["action"]);
+                Assert.AreEqual(1, personaController.TempData.Count);
+                Assert.AreEqual("Se ha eliminado correctamente a la persona", personaController.TempData["notification"]);
+                Assert.IsNull(personaController.TempData["warning"]);
+                Assert.IsNull(personaController.TempData["error"]);
+            }
+        }
+
+        [Test]
+        public void PersonaQuitarGET_PersonaInexistente()
+        {
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+
+            using (new RequestEmulator(NhHelper.GetSessionFactory()))
+            {
+                const string personaId = "11111111-1111-1111-1111-111111111111";
+                var resultVer = personaController.Quitar(personaId);
+                Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultVer);
+                Assert.AreEqual("Index", ((RedirectToRouteResult)resultVer).RouteValues["action"]);
+                Assert.AreEqual(string.Format("No se pudo encontrar la persona cuyo Id sea {0}", personaId), personaController.TempData["error"]);
+            }
+        }
+
+        [Test]
+        public void PersonaVerGET()
+        {
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+            PersonaNueva(ref personaController);
+
+            using (new RequestEmulator(NhHelper.GetSessionFactory()))
+            {
+                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
+
+                var resultModificar = personaController.Ver(personaRepository.GetAll().FirstOrDefault().Id.ToString());
+                Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
+                Assert.AreEqual("DefaultViewer", ((ViewResult)resultModificar).ViewName);
+            }
+        }
+
+        [Test]
+        public void PersonaVerGET_PersonaInexistente()
+        {
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+
+            using (new RequestEmulator(NhHelper.GetSessionFactory()))
+            {
+                var resultVer = personaController.Ver("11111111-1111-1111-1111-111111111111");
+                Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultVer);
+                Assert.AreEqual("Index", ((RedirectToRouteResult)resultVer).RouteValues["action"]);
+                Assert.AreEqual("No se encontrĂ³ la persona que intenta visualizar", personaController.TempData["error"]);
+            }
+        }
     }
 }