changeset 261:d669e9b9e24e

PersonaControllerTests: No se estaba pasando el HttpContext al test.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 13:59:18 -0300
parents 8a2a58b5e2d6
children b87a4875b2b6
files Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs
diffstat 1 files changed, 49 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Thu Oct 20 08:27:52 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Sat Oct 22 13:59:18 2011 -0300
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Web;
 using System.Web.Mvc;
 using Agendas.NHibernate;
 using Agendas.Repositories.Tests.Infraestructure;
@@ -16,6 +17,9 @@
     [TestFixture]
     public class PersonaControllerTests
     {
+        readonly Mock<HttpContextBase> _httpCtxStub = new Mock<HttpContextBase>();
+        readonly ControllerContext _controllerCtx = new ControllerContext();
+
         private void SetearUsuario()
         {
             var seguridad = new Mock<ISeguridad>();
@@ -29,37 +33,39 @@
             NhHelperTest.CleanDb();
             //PopulatePersona();
             SetearUsuario();
+            _controllerCtx.HttpContext = _httpCtxStub.Object;
         }
 
         [Test]
         public void PersonaNueva_ErrorAlGuardarTwitterConArroba()
         {
-            var personaController = new PersonaController();
+            var personaController = new PersonaController { ControllerContext = _controllerCtx };
 
-            var personaNew = new PersonaNewModel
-                                 {
-                                     Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/",
-                                     EMail = "juanjose.montesdeocaarbos gmail.com",
-                                     Nombre = "Juan José Montes de Oca Arbós",
-                                     Roles = new string[] {"Administrador", "Usuario"},
-                                     Twitter = "@jjmoa"
-                                 };
+            var form = new FormCollection
+                           {
+                               {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
+                               {"EMail", "juanjose.montesdeocaarbos gmail.com"},
+                               {"Nombre", "Juan José Montes de Oca Arbós"},
+                               {"Roles", "Administrador"},
+                               {"Roles", "Usuario"},
+                               {"Twitter", "@jjmoa"}
+                           };
+            var personaNew = BindModel<PersonaNewModel>(personaController, form);
 
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
-                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
                 var resultNuevo = personaController.Nueva(personaNew);
                 Assert.IsInstanceOf(typeof (ViewResult), resultNuevo);
                 Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName);
-                Assert.IsTrue(personaController.TempData.ContainsKey("error"));
-                Assert.AreNotEqual(string.Empty, personaController.TempData["error"]);
+                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
             }
         }
 
         [Test]
         public void PersonaModificar_ErrorAlGuardarTwitterConArroba()
         {
-            var personaController = new PersonaController();
+            var personaController = new PersonaController {ControllerContext = _controllerCtx};
+
             #region PersonaNew
             var personaNew = new PersonaNewModel
                                  {
@@ -80,25 +86,43 @@
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
                 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
-                personaEdit = new PersonaEditModel
-                                      {
-                                          Id = personaRepository.GetAll().FirstOrDefault().Id.ToString(),
-                                          Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/",
-                                          EMail = "juanjose.montesdeocaarbos gmail.com",
-                                          Nombre = "Juan José Montes de Oca Arbós",
-                                          Roles = new string[] {"Administrador", "Usuario"},
-                                          Twitter = "@jjmoa"
-                                      };
+
+                var form = new FormCollection
+                               {
+                                   {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
+                                   {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
+                                   {"EMail", "juanjose.montesdeocaarbos gmail.com"},
+                                   {"Nombre", "Juan José Montes de Oca Arbós"},
+                                   {"Roles", "Administrador"},
+                                   {"Roles", "Usuario"},
+                                   {"Twitter", "@jjmoa"}
+                               };
+                personaEdit = BindModel<PersonaEditModel>(personaController, form);
             }
+            
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
-                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
                 var resultModificar = personaController.Modificar(personaEdit);
                 Assert.IsInstanceOf(typeof (ViewResult), resultModificar);
                 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName);
-                Assert.IsTrue(personaController.TempData.ContainsKey("error"));
-                Assert.AreNotEqual(string.Empty, personaController.TempData["error"]);
+                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
             }
         }
+
+        protected static TModel BindModel<TModel>(Controller controller, IValueProvider valueProvider) where TModel : class
+        {
+            IModelBinder binder = ModelBinders.Binders.GetBinder(typeof(TModel));
+            ModelBindingContext bindingContext = new ModelBindingContext()
+            {
+                FallbackToEmptyPrefix = true,
+                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)),
+                ModelName = "NotUsedButNotNull",
+                ModelState = controller.ModelState,
+                PropertyFilter = (name => { return true; }),
+                ValueProvider = valueProvider
+            };
+
+            return (TModel)binder.BindModel(controller.ControllerContext, bindingContext);
+        }
     }
 }