diff Agendas/trunk/src/Agendas.Web.Tests/Controllers/MvcControllerTestsBase.cs @ 262:b87a4875b2b6

ControllerTests: Refactoring.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 14:08:30 -0300
parents
children 3339d1da6a63
line wrap: on
line diff
--- /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);
+        }
+    }
+}