title: How to mock MVC form POST with Moq
body: <p>Could anyone please tell how to mock a MVC form post using Moq?</p>
  
  <p>All I want is to unit test my method based on few different form posts.</p>
  
  <p>I tried to Google for this and there isn't a top to bottom guide out there.</p>
  
  <p>Thanks</p>
  
  <p>EDIT: adding code</p>
  
  <pre><code>    [TestMethod]
      public void SubscriptionControllerTest()
      {
          var subscriptionViewModel = new SubscriptionViewModel();
  
          //HTTP REQUEST SET UP
          var httpRequest = new Mock&lt;HttpRequestBase&gt;();
  
          httpRequest.Setup(r =&gt; r.Path).Returns("/Subscription/SendEmail");
  
          httpRequest.Setup(r =&gt; r.Form).Returns(delegate()
          {
              var nv = new NameValueCollection();
              nv.Add("FirstName", "John");
              nv.Add("LastName", "Smith");
              nv.Add("Email", "jsmith@host.com");
              nv.Add("Comments", "Comments are here...");
              nv.Add("ReceiveUpdates", "true");
              return nv;
          });
  
  
          //HTTP CONTEXT SET UP
          var httpContext = new Mock&lt;HttpContextBase&gt;();
  
          httpContext.Setup(c =&gt; c.Request).Returns(httpRequest.Object);
  
          var subscriptionController = new Mock&lt;SubscriptionController&gt;();
          subscriptionController.Setup(s =&gt; s.HttpContext).Returns(httpContext.Object);
  
          var result = subscriptionController.Object.SendEmail(subscriptionViewModel);
  
          Assert.AreEqual(((ViewResult)result).ViewName, "Index");
  
      }
  }
  </code></pre>
  
  <p>I have a controller called SubscriptionController.
  In there, I have a Action Method called SendEmail.
  I want to be able to run my Subscription/SendEmail using this Test method above. 
  My view is a form with the following fields: First Name, Last Name, Email, Comments and a checkbox.
  I need to mock this form and also mock my controller, http request and context. I am a bit confused what to mock and what to use as real. Thanks for any clarification.</p>