view 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 source

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);
        }
    }
}