title: How to Mock Request object in unit testing asp.net mvc application
body: <p>I am working on an asp.net mvc 3.0 application. In unit testing one of the action method in my controller, I was getting an error.</p>
  
  <p>How to mock: <code>Request.Params["FieldName"]</code></p>
  
  <p>I have included Moq framework, but was not sure how to pass value </p>
  
  <p>Here is my code... Please suggest...</p>
  
  <pre><code> var request = new Mock&lt;System.Web.HttpRequestBase&gt;();
  
   request
       .SetupGet(x =&gt; x.Headers)
       .Returns(
           new System.Net.WebHeaderCollection
           {
               {"X-Requested-With", "XMLHttpRequest"}
           });
  
   var context = new Mock&lt;System.Web.HttpContextBase&gt;();
  
   context.SetupGet(x =&gt; x.Request).Returns(request.Object);
  
   ValidCodeController target = new ValidCodeController();
  
   target.ControllerContext =
       new ControllerContext(context.Object, new RouteData(), target);
  </code></pre>