HttpContext --- What it does ? (MVC View Unit Testing)

Last post 11-11-2008 5:19 AM by foiaz. 2 replies.

Sort Posts:

  • Party!!! [<:o)] HttpContext --- What it does ? (MVC View Unit Testing)

    11-10-2008, 6:56 AM
    • Member
      point Member
    • foiaz
    • Member since 10-28-2008, 9:51 AM
    • Posts 6

    Hi all,

    I have a problem with the following code.. I am trying write unit test the view pages of MV... My code is executing without an error but it returns null value when call the RenderView method... Can any one please help me with this ? My code is as follows.

     

             private static string RenderView(string controllerName, string viewName, string queryString, object viewData)
            {
                StringBuilder result = new StringBuilder();
                string virtualPath = string.Format("/{0}/{1}", controllerName, viewName);

                 HttpContextWrapper context = CreateHttpContext(result, virtualPath, queryString);
                ControllerContext controllerContext = CreateControllerContext(context, controllerName, viewName);
                ViewContext viewContext = new ViewContext(controllerContext, new Mock<IView>().Object, new ViewDataDictionary(viewData), new TempDataDictionary());
                ViewEngineResult vrs = CreateView(viewName, viewData, controllerContext);

                //vrs.View.Render(viewContext, new StringWriter(result));
                //viewContext.View.Render(viewContext, new StringWriter(result));
                context.Response.Flush();

                return result.ToString();
            }

           private static HttpContextWrapper CreateHttpContext(StringBuilder result, string virtualPath, string queryString)
            {
                SimpleWorkerRequest wr = new SimpleWorkerRequest(virtualPath, queryString, new StringWriter(result));
                HttpContext.Current = new HttpContext(wr);
                return new HttpContextWrapper(HttpContext.Current);
            }

            private static ControllerContext CreateControllerContext(HttpContextWrapper context, string controllerName, string actionName)
            {
                RouteTable.Routes.Clear();
                RouteTable.Routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
                                          {
                                                Defaults = new RouteValueDictionary(new { action = "Index", id = "" })
                                          });
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", controllerName);
                routeData.Values.Add("action", actionName);
               
                MockRepository mocks = new MockRepository();
                return new ControllerContext(context, routeData, mocks.DynamicMock<ControllerBase>());
            }
            private static ViewEngineResult CreateView(string viewName, object viewData, ControllerContext controllerContext)
            {
                CreateViewCL viewFactory = new CreateViewCL();
                return viewFactory.FindView(controllerContext, viewName, "Site.Master");
            }

     

    Thanks in advance.

  • Re: HttpContext --- What it does ? (MVC View Unit Testing)

    11-11-2008, 4:56 AM
    • Member
      122 point Member
    • mvcian
    • Member since 10-29-2008, 11:52 AM
    • Chennai
    • Posts 42

    Hi,

      It is simpler to do same thing in MVC Beta . Why can't you give a try?

    regards,

                 mvcian

     

  • Re: HttpContext --- What it does ? (MVC View Unit Testing)

    11-11-2008, 5:19 AM
    • Member
      point Member
    • foiaz
    • Member since 10-28-2008, 9:51 AM
    • Posts 6

    Hi Thanks for your reply,

     Am trying that with MVC Beta only, I got this example from codeproject.com (http://www.codeproject.com/KB/aspnet/Unit_Testing_MVC_Views.aspx).. and I tried to modify it according to MVC beta

     If you look at that code.. you can find that the instance which i created for ViewContext belongs to MVC beta... It ll take four parameters rather than three in earlier release...

     code is executing without any error but i didn't get any output..

      Am doubting about the instance which I created for HttpContextWrapper.... because its resultant value is becoming "null".

Page 1 of 1 (3 items)