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

  • 相关阅读:
    序言vue.js介绍
    python中end=''
    python文件的操作
    python异常
    python异常(理论知识)
    Uva 11300 Spreading the Wealth 中位数
    反转(开关问题) poj3276
    NEKO's Maze Game 思维
    Aaronson 一道思维题
    我开博客了
  • 原文地址:https://www.cnblogs.com/ycdx2001/p/1429111.html
Copyright © 2011-2022 走看看