Mvc Moq HttpContext

   1: public class MockMvcHttpContext
   2: {
   3:     public Moq.Mock<System.Web.HttpContextBase> Context { get; set; }
   4:  
   5:     public Moq.Mock<System.Web.HttpRequestBase> Request { get; set; }
   6:  
   7:     public Moq.Mock<System.Web.HttpResponseBase> Response { get; set; }
   8:  
   9:     public System.Web.HttpCookieCollection Cookie { get; set; }
  10:  
  11:     public System.Web.Mvc.Controller Controller { get; set; }
  12:  
  13:     private System.Collections.Specialized.NameValueCollection _form;
  14:     public System.Collections.Specialized.NameValueCollection Form {
  15:         get
  16:         {
  17:             return _form;
  18:         }
  19:         set
  20:         {
  21:             _form = value;
  22:             this.Controller.ValueProvider = new System.Web.Mvc.FormCollection(value).ToValueProvider();
  23:         }
  24:     }
  25:     public System.Collections.Specialized.NameValueCollection Query { get; set; }
  26: }
  27:  
  28: public static class MvcTestExtensions
  29: {
  30:     public static MockMvcHttpContext SetupFullContext(this System.Web.Mvc.Controller controller)
  31:     {
  32:         return controller.SetupContext().SetupRequest().SetupResponse().SetupCookie().SetupForm().SetupQuerystring();
  33:     }
  34:  
  35:     public static MockMvcHttpContext SetupContext(this System.Web.Mvc.Controller controller)
  36:     {
  37:         
  38:         var context = new Moq.Mock<System.Web.HttpContextBase>();
  39:         controller.ControllerContext = new System.Web.Mvc.ControllerContext(context.Object, new System.Web.Routing.RouteData(), controller);
  40:         var obj = new MockMvcHttpContext()
  41:         {
  42:             Controller = controller,
  43:             Context=context
  44:         };
  45:         return obj;
  46:     }
  47:  
  48:     public static MockMvcHttpContext SetupRequest(this MockMvcHttpContext context)
  49:     {
  50:         if (context.Context == null)
  51:         {
  52:             throw new System.ArgumentNullException("context.Context");
  53:         }
  54:         var request = new Moq.Mock<System.Web.HttpRequestBase>();
  55:         context.Context.SetupGet(x => x.Request).Returns(request.Object);
  56:         context.Request = request;
  57:         return context;
  58:     }
  59:  
  60:     public static MockMvcHttpContext SetupResponse(this MockMvcHttpContext context)
  61:     {
  62:         if (context.Context == null)
  63:         {
  64:             throw new System.ArgumentNullException("context.Context");
  65:         }
  66:         var response = new Moq.Mock<System.Web.HttpResponseBase>();
  67:         context.Context.SetupGet(x => x.Response).Returns(response.Object);
  68:         context.Response = response;
  69:         return context;
  70:     }
  71:  
  72:     public static MockMvcHttpContext SetupCookie(this MockMvcHttpContext context)
  73:     {
  74:         if (context.Context == null)
  75:         {
  76:             throw new System.ArgumentNullException("context.Context");
  77:         }
  78:         var cookie = new System.Web.HttpCookieCollection();
  79:         if (context.Response != null)
  80:         {
  81:             context.Response.SetupGet(x => x.Cookies).Returns(cookie);
  82:         }
  83:         if (context.Request != null)
  84:         {
  85:             context.Request.SetupGet(x => x.Cookies).Returns(cookie);
  86:         }
  87:         context.Cookie = cookie;
  88:         
  89:         return context;
  90:     }
  91:  
  92:     public static MockMvcHttpContext SetupForm(this MockMvcHttpContext context)
  93:     {
  94:         if (context.Context == null)
  95:         {
  96:             throw new System.ArgumentNullException("context.Context");
  97:         }
  98:         
  99:         if (context.Request == null)
 100:         {
 101:             throw new System.ArgumentNullException("context.Request");
 102:         }
 103:         var form = new System.Collections.Specialized.NameValueCollection();
 104:         context.Request.SetupGet(x => x.Form).Returns(form);
 105:         context.Form = form;
 106:         return context;
 107:     }
 108:  
 109:     public static MockMvcHttpContext SetupQuerystring(this MockMvcHttpContext context)
 110:     {
 111:         if (context.Context == null)
 112:         {
 113:             throw new System.ArgumentNullException("context.Context");
 114:         }
 115:  
 116:         if (context.Request == null)
 117:         {
 118:             throw new System.ArgumentNullException("context.Request");
 119:         }
 120:         var query = new System.Collections.Specialized.NameValueCollection();
 121:         context.Request.SetupGet(x => x.QueryString).Returns(query);
 122:         context.Query = query;
 123:         return context;
 124:     }
 125: }

需要引入Moq库

 

useage:

controller.SetupFullContext()

你可能感兴趣的:(context)