# HG changeset patch # User juanjose.montesdeocaarbos # Date 1319302758 10800 # Node ID d669e9b9e24ee15008a92a87fe1e57b09e30a54c # Parent 8a2a58b5e2d6701768ef3bd68ea8c0914746c969 PersonaControllerTests: No se estaba pasando el HttpContext al test. diff -r 8a2a58b5e2d6 -r d669e9b9e24e Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs --- 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 _httpCtxStub = new Mock(); + readonly ControllerContext _controllerCtx = new ControllerContext(); + private void SetearUsuario() { var seguridad = new Mock(); @@ -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(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(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(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); + } } }