title: How do you set ApiController.User
body: <p>In my apicontroller I use base.user to identify the authenticated user to use in a lookup.  Now I am writing a unit test for this but I cannot figure out how to mock apicontroller.user.  Do I need to create a request and set the user there?  Or is there another way to set the controller.user?</p>
  
  <p>Here is my controller; I have already mocked repository and membershipservice.</p>
  
  <pre><code>[Authorize]
  public class DocumentController : ApiController
  {
      DocumentRepository _repository;
      IStaticMembershipService _membership;
  
      public IEnumerable&lt;Document&gt; GetDocuments()
      {
          MembershipUser userAccount = _membership.GetUser(base.User);
          IEnumerable&lt;Document&gt; docs = null;
  
          if (userAccount != null)
          {
              docs = _repository.GetDocumentsByUserId(
                  (Guid) userAccount.ProviderUserKey);
          }
  
          return docs;
      }
  </code></pre>
  
  <p>Here is my unit test:</p>
  
  <pre><code>[TestClass]
  public class DocumentControllerWebService
  {
      private DocumentsContext _context;
      private DocumentRepository _repository;
      private DocumentController _controller;
      private FakeMembershipService _membership;
      private TestContext testContextInstance;
  
      [TestInitialize]
      public void MyTestInitialize()
      {
          // Create a context with a fake data set provider
          _context = new DocumentsContext(new FakeDbSetProvider());
          _repository = new DocumentRepository(_context);
          _membership = new FakeMembershipService();
          _controller = new DocumentController(_repository, _membership);
      }
  
      public void GetDocumentsTest()
      {
          string userName = "someUser";
          MembershipUser userAccount = _membership.GetUser(userName);
          Guid userId = (Guid) userAccount.ProviderUserKey;
          Guid anotherUserId = Guid.NewGuid();
  
          // Get some dummy data and insert it into the fake repository
          List&lt;Document&gt; forms = DocumentDummyData.GetListOfDummyData(
              userId, anotherUserId);
          forms.ForEach(f =&gt; _repository.InsertDocument(f));
  
          // I would like to do this but User is readonly
          _controller.User = userName;
  
          List&lt;Document&gt; docs = _controller.GetDocuments().ToList();
          foreach (Document expected in forms.Where(d =&gt; d.UserId == userId))
          {
              Document actual = docs.Where(
                  d =&gt; d.DocumentID == expected.DocumentID).FirstOrDefault();
              Assert.IsNotNull(actual);
              Assert.AreEqual(expected.DocumentID, actual.DocumentID);
          }
      }
  }
  </code></pre>