title: How to unit test POST method
body: <p>I have problems with testing post method.
  In that method AntiForgeryToken is checked and I don't know to how to mock it.
  Beside that everythings works.</p>
  
  <p>Here my method I want to test:</p>
  
  <pre><code>[HttpPost]
      //[ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include="Id,Name,Manufacturer,CatalogNo")] Device device)
      {
          ValidateRequestHeader(Request);
          if (String.IsNullOrWhiteSpace(device.Name)||String.IsNullOrWhiteSpace(device.Manufacturer)||String.IsNullOrWhiteSpace(device.CatalogNo))
          {
              ModelState.AddModelError("", "Niepoprawne dane");
              return PartialView(device);
          }
          unitOfWork.deviceRepository.Insert(device);
          unitOfWork.Save();
          return Json(new { ok = true, newurl = Url.Action("Index") });
      }
  </code></pre>
  
  <p>I have already mocked:</p>
  
  <pre><code>        private IUnitOfWork fakeRepo;
      DeviceController DC;
      [TestInitialize]
      public void Initialize()
      {
          Mock&lt;IUnitOfWork&gt; mock = new Mock&lt;IUnitOfWork&gt;();
          mock.Setup(m =&gt; m.deviceRepository.Get(
              It.IsAny&lt;List&lt;Expression&lt;Func&lt;Device, bool&gt;&gt;&gt;&gt;(),
              It.IsAny&lt;Func&lt;IQueryable&lt;Device&gt;, IOrderedQueryable&lt;Device&gt;&gt;&gt;(),
              null))
              .Returns(new[] { new Device { Id = 1, Manufacturer = "a", Name = "b", CatalogNo = "x" } });
          mock.Setup(m =&gt; m.deviceRepository.Get()).Returns(new[] { new Device { Id = 1, Manufacturer = "a", Name = "b", CatalogNo = "z" } });
          fakeRepo = mock.Object;
          DC = new DeviceController(fakeRepo);
      }
  </code></pre>
  
  <p>But I'dont know how to method testing <code>AntiforgeryToken</code> 
  here code:</p>
  
  <pre><code>public void ValidateRequestHeader(HttpRequestBase request)
      {
          string cookieToken = "";
          string formToken = "";
  
          if (request.Headers["RequestVerificationToken"] != null)
          {
              string[] tokens = request.Headers["RequestVerificationToken"].Split(':');
              if (tokens.Length == 2)
              {
                  cookieToken = tokens[0].Trim();
                  formToken = tokens[1].Trim();
              }
          }
          AntiForgery.Validate(cookieToken, formToken);
      }
  </code></pre>
  
  <p>I can't comment out this call testing purpose but this solution seems little ugly.</p>
  
  <p>Can You suggest any edits?</p>