Mercurial > altnet-hispano
comparison Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 261:d669e9b9e24e
PersonaControllerTests: No se estaba pasando el HttpContext al test.
author | juanjose.montesdeocaarbos |
---|---|
date | Sat, 22 Oct 2011 13:59:18 -0300 |
parents | e6c041d8e1bf |
children | b87a4875b2b6 |
comparison
equal
deleted
inserted
replaced
260:8a2a58b5e2d6 | 261:d669e9b9e24e |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.Linq; | 3 using System.Linq; |
4 using System.Web; | |
4 using System.Web.Mvc; | 5 using System.Web.Mvc; |
5 using Agendas.NHibernate; | 6 using Agendas.NHibernate; |
6 using Agendas.Repositories.Tests.Infraestructure; | 7 using Agendas.Repositories.Tests.Infraestructure; |
7 using AltNetHispano.Agendas.Domain; | 8 using AltNetHispano.Agendas.Domain; |
8 using AltNetHispano.Agendas.Repositories.NHibernate; | 9 using AltNetHispano.Agendas.Repositories.NHibernate; |
14 namespace Agendas.Web.Tests.Controllers | 15 namespace Agendas.Web.Tests.Controllers |
15 { | 16 { |
16 [TestFixture] | 17 [TestFixture] |
17 public class PersonaControllerTests | 18 public class PersonaControllerTests |
18 { | 19 { |
20 readonly Mock<HttpContextBase> _httpCtxStub = new Mock<HttpContextBase>(); | |
21 readonly ControllerContext _controllerCtx = new ControllerContext(); | |
22 | |
19 private void SetearUsuario() | 23 private void SetearUsuario() |
20 { | 24 { |
21 var seguridad = new Mock<ISeguridad>(); | 25 var seguridad = new Mock<ISeguridad>(); |
22 seguridad.Setup(s => s.GetUserName()).Returns("neluz"); | 26 seguridad.Setup(s => s.GetUserName()).Returns("neluz"); |
23 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); | 27 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); |
27 public void Setup() | 31 public void Setup() |
28 { | 32 { |
29 NhHelperTest.CleanDb(); | 33 NhHelperTest.CleanDb(); |
30 //PopulatePersona(); | 34 //PopulatePersona(); |
31 SetearUsuario(); | 35 SetearUsuario(); |
36 _controllerCtx.HttpContext = _httpCtxStub.Object; | |
32 } | 37 } |
33 | 38 |
34 [Test] | 39 [Test] |
35 public void PersonaNueva_ErrorAlGuardarTwitterConArroba() | 40 public void PersonaNueva_ErrorAlGuardarTwitterConArroba() |
36 { | 41 { |
37 var personaController = new PersonaController(); | 42 var personaController = new PersonaController { ControllerContext = _controllerCtx }; |
38 | 43 |
39 var personaNew = new PersonaNewModel | 44 var form = new FormCollection |
40 { | 45 { |
41 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", | 46 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"}, |
42 EMail = "juanjose.montesdeocaarbos gmail.com", | 47 {"EMail", "juanjose.montesdeocaarbos gmail.com"}, |
43 Nombre = "Juan José Montes de Oca Arbós", | 48 {"Nombre", "Juan José Montes de Oca Arbós"}, |
44 Roles = new string[] {"Administrador", "Usuario"}, | 49 {"Roles", "Administrador"}, |
45 Twitter = "@jjmoa" | 50 {"Roles", "Usuario"}, |
46 }; | 51 {"Twitter", "@jjmoa"} |
52 }; | |
53 var personaNew = BindModel<PersonaNewModel>(personaController, form); | |
47 | 54 |
48 using (new RequestEmulator(NhHelper.GetSessionFactory())) | 55 using (new RequestEmulator(NhHelper.GetSessionFactory())) |
49 { | 56 { |
50 Assert.IsFalse(personaController.TempData.ContainsKey("error")); | |
51 var resultNuevo = personaController.Nueva(personaNew); | 57 var resultNuevo = personaController.Nueva(personaNew); |
52 Assert.IsInstanceOf(typeof (ViewResult), resultNuevo); | 58 Assert.IsInstanceOf(typeof (ViewResult), resultNuevo); |
53 Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName); | 59 Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName); |
54 Assert.IsTrue(personaController.TempData.ContainsKey("error")); | 60 Assert.IsFalse(personaController.TempData.ContainsKey("error")); |
55 Assert.AreNotEqual(string.Empty, personaController.TempData["error"]); | |
56 } | 61 } |
57 } | 62 } |
58 | 63 |
59 [Test] | 64 [Test] |
60 public void PersonaModificar_ErrorAlGuardarTwitterConArroba() | 65 public void PersonaModificar_ErrorAlGuardarTwitterConArroba() |
61 { | 66 { |
62 var personaController = new PersonaController(); | 67 var personaController = new PersonaController {ControllerContext = _controllerCtx}; |
68 | |
63 #region PersonaNew | 69 #region PersonaNew |
64 var personaNew = new PersonaNewModel | 70 var personaNew = new PersonaNewModel |
65 { | 71 { |
66 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", | 72 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", |
67 EMail = "juanjose.montesdeocaarbos@gmail.com", | 73 EMail = "juanjose.montesdeocaarbos@gmail.com", |
78 | 84 |
79 PersonaEditModel personaEdit = null; | 85 PersonaEditModel personaEdit = null; |
80 using (new RequestEmulator(NhHelper.GetSessionFactory())) | 86 using (new RequestEmulator(NhHelper.GetSessionFactory())) |
81 { | 87 { |
82 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); | 88 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); |
83 personaEdit = new PersonaEditModel | 89 |
84 { | 90 var form = new FormCollection |
85 Id = personaRepository.GetAll().FirstOrDefault().Id.ToString(), | 91 { |
86 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", | 92 {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()}, |
87 EMail = "juanjose.montesdeocaarbos gmail.com", | 93 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"}, |
88 Nombre = "Juan José Montes de Oca Arbós", | 94 {"EMail", "juanjose.montesdeocaarbos gmail.com"}, |
89 Roles = new string[] {"Administrador", "Usuario"}, | 95 {"Nombre", "Juan José Montes de Oca Arbós"}, |
90 Twitter = "@jjmoa" | 96 {"Roles", "Administrador"}, |
91 }; | 97 {"Roles", "Usuario"}, |
98 {"Twitter", "@jjmoa"} | |
99 }; | |
100 personaEdit = BindModel<PersonaEditModel>(personaController, form); | |
92 } | 101 } |
102 | |
93 using (new RequestEmulator(NhHelper.GetSessionFactory())) | 103 using (new RequestEmulator(NhHelper.GetSessionFactory())) |
94 { | 104 { |
95 Assert.IsFalse(personaController.TempData.ContainsKey("error")); | |
96 var resultModificar = personaController.Modificar(personaEdit); | 105 var resultModificar = personaController.Modificar(personaEdit); |
97 Assert.IsInstanceOf(typeof (ViewResult), resultModificar); | 106 Assert.IsInstanceOf(typeof (ViewResult), resultModificar); |
98 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName); | 107 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName); |
99 Assert.IsTrue(personaController.TempData.ContainsKey("error")); | 108 Assert.IsFalse(personaController.TempData.ContainsKey("error")); |
100 Assert.AreNotEqual(string.Empty, personaController.TempData["error"]); | |
101 } | 109 } |
110 } | |
111 | |
112 protected static TModel BindModel<TModel>(Controller controller, IValueProvider valueProvider) where TModel : class | |
113 { | |
114 IModelBinder binder = ModelBinders.Binders.GetBinder(typeof(TModel)); | |
115 ModelBindingContext bindingContext = new ModelBindingContext() | |
116 { | |
117 FallbackToEmptyPrefix = true, | |
118 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)), | |
119 ModelName = "NotUsedButNotNull", | |
120 ModelState = controller.ModelState, | |
121 PropertyFilter = (name => { return true; }), | |
122 ValueProvider = valueProvider | |
123 }; | |
124 | |
125 return (TModel)binder.BindModel(controller.ControllerContext, bindingContext); | |
102 } | 126 } |
103 } | 127 } |
104 } | 128 } |