I am trying to use unit test code using NUnit. I have a method:
public static string RenderRoute(HttpContextBase context, RouteValueDictionary values) { var routeData = new RouteData(); foreach (var kvp in values) { routeData.Values.Add(kvp.Key, kvp.Value); } string controllerName = routeData.GetRequiredString("controller"); var requestContext = new RequestContext(context, routeData); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); var ActionInvoker = new ControllerActionInvoker(); var controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); ((ControllerBase)controller).ControllerContext = controllerContext; string actionName = routeData.GetRequiredString("action"); Action action = delegate { ActionInvoker.InvokeAction(controllerContext, actionName); }; return new BlockRenderer(context).Capture(action); }
My default controller is the StructureMap factory controller from MvcContrib. I also use MvcMockHelpers from MvcContrib to help me make fun of HttpContextBase.
The controller I'm trying to test calls the above RenderRoute method and explodes at:
IController controller = factory.CreateController(requestContext, controllerName);
With an error:
Controllers.WidgetControllerTests.CanCreateWidgetOnPage: System.Web.HttpException: The type initializer for "System.Web.Compilation.CompilationLock" made an exception. ----> System.TypeInitializationException: the type initializer for "System.Web.Compilation.CompilationLock" made an exception. ----> System.NullReferenceException: the reference to the object is not installed in the instance of the object.
I am new to unit testing / bullying and it is an opportunity that I don't see anything simple.
Here is the test I'm doing now:
[Test] public void Test() { HttpContextBase context = MvcMockHelpers.DynamicHttpContextBase(); string s = RenderExtensions.RenderAction<HomeController>(context, a => a.About()); Console.WriteLine(s); Assert.IsNotNullOrEmpty(s); }
Any help would be appreciated.
I simplified the problem to this simple unit test:
[Test] public void Test2() { HttpContextBase context = MvcMockHelpers.DynamicHttpContextBase(); var routeData = new RouteData(); routeData.Values.Add("Controller", "Home"); routeData.Values.Add("Action", "About"); string controllerName = routeData.GetRequiredString("controller"); var requestContext = new RequestContext(context, routeData); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); Assert.IsNotNull(controller); }
c # asp.net-mvc
justin
source share