annotate 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
rev   line source
262
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
1 using System.Web;
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
2 using System.Web.Mvc;
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
3 using Moq;
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
4
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
5 namespace Agendas.Web.Tests.Controllers
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
6 {
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
7 public class MvcControllerTestsBase
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
8 {
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
9 protected readonly Mock<HttpContextBase> HttpCtxStub = new Mock<HttpContextBase>();
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
10 protected readonly ControllerContext ControllerCtx = new ControllerContext();
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
11
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
12 protected void SetupTests()
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
13 {
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
14 ControllerCtx.HttpContext = HttpCtxStub.Object;
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
15 }
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
16
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
17 protected static TModel BindModel<TModel>(Controller controller, IValueProvider valueProvider) where TModel : class
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
18 {
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
19 var binder = ModelBinders.Binders.GetBinder(typeof(TModel));
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
20 var bindingContext = new ModelBindingContext()
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
21 {
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
22 FallbackToEmptyPrefix = true,
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
23 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)),
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
24 ModelName = "NotUsedButNotNull",
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
25 ModelState = controller.ModelState,
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
26 PropertyFilter = (name => true),
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
27 ValueProvider = valueProvider
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
28 };
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
29
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
30 return (TModel)binder.BindModel(controller.ControllerContext, bindingContext);
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
31 }
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
32 }
b87a4875b2b6 ControllerTests: Refactoring.
juanjose.montesdeocaarbos
parents:
diff changeset
33 }