changeset 262:b87a4875b2b6

ControllerTests: Refactoring.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 14:08:30 -0300
parents d669e9b9e24e
children 3339d1da6a63
files Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Agendas/trunk/src/Agendas.Web.Tests/Controllers/MvcControllerTestsBase.cs Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs
diffstat 3 files changed, 38 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj	Sat Oct 22 13:59:18 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj	Sat Oct 22 14:08:30 2011 -0300
@@ -57,6 +57,7 @@
   <ItemGroup>
     <Compile Include="AutorizationsTests.cs" />
     <Compile Include="Controllers\EventoControllerTests.cs" />
+    <Compile Include="Controllers\MvcControllerTestsBase.cs" />
     <Compile Include="Controllers\PersonaControllerTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/MvcControllerTestsBase.cs	Sat Oct 22 14:08:30 2011 -0300
@@ -0,0 +1,33 @@
+using System.Web;
+using System.Web.Mvc;
+using Moq;
+
+namespace Agendas.Web.Tests.Controllers
+{
+    public class MvcControllerTestsBase
+    {
+        protected readonly Mock<HttpContextBase> HttpCtxStub = new Mock<HttpContextBase>();
+        protected readonly ControllerContext ControllerCtx = new ControllerContext();
+
+        protected void SetupTests()
+        {
+            ControllerCtx.HttpContext = HttpCtxStub.Object;
+        }
+
+        protected static TModel BindModel<TModel>(Controller controller, IValueProvider valueProvider) where TModel : class
+        {
+            var binder = ModelBinders.Binders.GetBinder(typeof(TModel));
+            var bindingContext = new ModelBindingContext()
+            {
+                FallbackToEmptyPrefix = true,
+                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)),
+                ModelName = "NotUsedButNotNull",
+                ModelState = controller.ModelState,
+                PropertyFilter = (name => true),
+                ValueProvider = valueProvider
+            };
+
+            return (TModel)binder.BindModel(controller.ControllerContext, bindingContext);
+        }
+    }
+}
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Sat Oct 22 13:59:18 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Sat Oct 22 14:08:30 2011 -0300
@@ -15,11 +15,8 @@
 namespace Agendas.Web.Tests.Controllers
 {
     [TestFixture]
-    public class PersonaControllerTests
+    public class PersonaControllerTests : MvcControllerTestsBase
     {
-        readonly Mock<HttpContextBase> _httpCtxStub = new Mock<HttpContextBase>();
-        readonly ControllerContext _controllerCtx = new ControllerContext();
-
         private void SetearUsuario()
         {
             var seguridad = new Mock<ISeguridad>();
@@ -33,13 +30,13 @@
             NhHelperTest.CleanDb();
             //PopulatePersona();
             SetearUsuario();
-            _controllerCtx.HttpContext = _httpCtxStub.Object;
+            SetupTests();
         }
 
         [Test]
         public void PersonaNueva_ErrorAlGuardarTwitterConArroba()
         {
-            var personaController = new PersonaController { ControllerContext = _controllerCtx };
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
 
             var form = new FormCollection
                            {
@@ -64,7 +61,7 @@
         [Test]
         public void PersonaModificar_ErrorAlGuardarTwitterConArroba()
         {
-            var personaController = new PersonaController {ControllerContext = _controllerCtx};
+            var personaController = new PersonaController {ControllerContext = ControllerCtx};
 
             #region PersonaNew
             var personaNew = new PersonaNewModel
@@ -108,21 +105,5 @@
                 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);
-        }
     }
 }