# HG changeset patch
# User juanjose.montesdeocaarbos
# Date 1319303310 10800
# Node ID b87a4875b2b62dd29b4c8c50f86dbe56a1e3669a
# Parent d669e9b9e24ee15008a92a87fe1e57b09e30a54c
ControllerTests: Refactoring.
diff -r d669e9b9e24e -r b87a4875b2b6 Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj
--- 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 @@
+
diff -r d669e9b9e24e -r b87a4875b2b6 Agendas/trunk/src/Agendas.Web.Tests/Controllers/MvcControllerTestsBase.cs
--- /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 HttpCtxStub = new Mock();
+ protected readonly ControllerContext ControllerCtx = new ControllerContext();
+
+ protected void SetupTests()
+ {
+ ControllerCtx.HttpContext = HttpCtxStub.Object;
+ }
+
+ protected static TModel BindModel(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);
+ }
+ }
+}
diff -r d669e9b9e24e -r b87a4875b2b6 Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs
--- 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 _httpCtxStub = new Mock();
- readonly ControllerContext _controllerCtx = new ControllerContext();
-
private void SetearUsuario()
{
var seguridad = new Mock();
@@ -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(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);
- }
}
}