comparison Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 262:b87a4875b2b6

ControllerTests: Refactoring.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 14:08:30 -0300
parents d669e9b9e24e
children 3339d1da6a63
comparison
equal deleted inserted replaced
261:d669e9b9e24e 262:b87a4875b2b6
13 using NUnit.Framework; 13 using NUnit.Framework;
14 14
15 namespace Agendas.Web.Tests.Controllers 15 namespace Agendas.Web.Tests.Controllers
16 { 16 {
17 [TestFixture] 17 [TestFixture]
18 public class PersonaControllerTests 18 public class PersonaControllerTests : MvcControllerTestsBase
19 { 19 {
20 readonly Mock<HttpContextBase> _httpCtxStub = new Mock<HttpContextBase>();
21 readonly ControllerContext _controllerCtx = new ControllerContext();
22
23 private void SetearUsuario() 20 private void SetearUsuario()
24 { 21 {
25 var seguridad = new Mock<ISeguridad>(); 22 var seguridad = new Mock<ISeguridad>();
26 seguridad.Setup(s => s.GetUserName()).Returns("neluz"); 23 seguridad.Setup(s => s.GetUserName()).Returns("neluz");
27 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); 24 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
31 public void Setup() 28 public void Setup()
32 { 29 {
33 NhHelperTest.CleanDb(); 30 NhHelperTest.CleanDb();
34 //PopulatePersona(); 31 //PopulatePersona();
35 SetearUsuario(); 32 SetearUsuario();
36 _controllerCtx.HttpContext = _httpCtxStub.Object; 33 SetupTests();
37 } 34 }
38 35
39 [Test] 36 [Test]
40 public void PersonaNueva_ErrorAlGuardarTwitterConArroba() 37 public void PersonaNueva_ErrorAlGuardarTwitterConArroba()
41 { 38 {
42 var personaController = new PersonaController { ControllerContext = _controllerCtx }; 39 var personaController = new PersonaController { ControllerContext = ControllerCtx };
43 40
44 var form = new FormCollection 41 var form = new FormCollection
45 { 42 {
46 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"}, 43 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
47 {"EMail", "juanjose.montesdeocaarbos gmail.com"}, 44 {"EMail", "juanjose.montesdeocaarbos gmail.com"},
62 } 59 }
63 60
64 [Test] 61 [Test]
65 public void PersonaModificar_ErrorAlGuardarTwitterConArroba() 62 public void PersonaModificar_ErrorAlGuardarTwitterConArroba()
66 { 63 {
67 var personaController = new PersonaController {ControllerContext = _controllerCtx}; 64 var personaController = new PersonaController {ControllerContext = ControllerCtx};
68 65
69 #region PersonaNew 66 #region PersonaNew
70 var personaNew = new PersonaNewModel 67 var personaNew = new PersonaNewModel
71 { 68 {
72 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", 69 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/",
106 Assert.IsInstanceOf(typeof (ViewResult), resultModificar); 103 Assert.IsInstanceOf(typeof (ViewResult), resultModificar);
107 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName); 104 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName);
108 Assert.IsFalse(personaController.TempData.ContainsKey("error")); 105 Assert.IsFalse(personaController.TempData.ContainsKey("error"));
109 } 106 }
110 } 107 }
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);
126 }
127 } 108 }
128 } 109 }