title: how to mock user identity when testing web api controller with moq and nunit?
body: <p>I would like to test web service call with user identity set ( pretending the user is logged in)</p>
  
  <p>I have checked numerous posts and stackoverflow answers but I didn't quite get the answer I want.</p>
  
  <p>The closest I got is this.</p>
  
  <pre><code>var context = new Mock&lt;HttpContextBase&gt;();
  var mockIdentity = new Mock&lt;IIdentity&gt;();
  context.SetupGet(x =&gt; x.User.Identity).Returns(mockIdentity.Object);
  mockIdentity.Setup(x =&gt; x.Name).Returns("abc");
  </code></pre>
  
  <p>And at this point, I presume I have to set the controllercontext and that context variable needs to be fed somehow.</p>
  
  <p>I have seen a lot of examples regarding MVCcontrollers but I couldn't find the one that works for an ApiController. Can someone please shed some light on this?</p>
  
  <p>Thanks!</p>