zoukankan      html  css  js  c++  java
  • ASP.net MVC Mock Context(上下文)

    ASP.net MVC Mock Context(上下文)

     var fakeContext = new FakeControllerContext(controller, new NameValueCollection(), new NameValueCollection());


              controller.ControllerContext = fakeContext;

                controller.Request.QueryString["s"] = ""


    或:

     var controllerContext = new FakeControllerContext(controller,

                    new RouteData(), "", "", new string[] { }, new NameValueCollection(),

                    new NameValueCollection(), new HttpCookieCollection(), new System.Web.SessionState.SessionStateItemCollection());

                controller.ControllerContext = controllerContext; 

    如果要访问Request[""],则加上:

     var mockRequst = new Mock<HttpRequestBase>();


                mockRequst.ExpectGet(r => r.ServerVariables).Returns(new NameValueCollection());

                mockRequst.ExpectGet(r => r.QueryString).Returns(new NameValueCollection());

                mockRequst.ExpectGet(r => r.Form).Returns(new NameValueCollection());

                mockRequst.ExpectGet(r => r.Cookies).Returns(new HttpCookieCollection());


                var mockHttpContext = new Mock<HttpContextBase>();

                mockHttpContext.ExpectGet(hc => hc.Request).Returns(mockRequst.Object);

                controller.ControllerContext.HttpContext = mockHttpContext.Object;


    namespace MvcFakes 


    在 BFA项目BFA.Presentation.Impl.Test.BuyerCompanyControllerTest.AjaxPartialManagementReturnExpectWhenOnDefault()用到

     下载:MvcFakes.rar

  • 相关阅读:
    BZOJ1054|HAOI2008移动玩具|广搜
    tarjan算法
    BJOJ2190|SDOI仪仗队|数论
    POJ2975|Nim|博弈论
    POJ1740|A NEW STONE GAME|博弈论
    python 单例模式
    linux 根据服务名称批量杀死进程
    python 任务计划
    python偏函数
    安装scrapy框架
  • 原文地址:https://www.cnblogs.com/ycdx2001/p/1429111.html
Copyright © 2011-2022 走看看