title: mocking User.Identity.Name in MVC4
body: <p>I am trying to write a test for one of my methods in controller (MVC4). I use Moq. Within the test method I create a mock for my repository like so:</p>
  
  <pre><code>Mock&lt;ISurveyRepository&gt; mock = new Mock&lt;ISurveyRepository&gt;();
  </code></pre>
  
  <p>and proceed with mocking up repository calls. First one of those is:</p>
  
  <pre><code>int userId = repository.GetUserId(User.Identity.Name);
  </code></pre>
  
  <p>So I add this to my test method:</p>
  
  <pre><code>mock.Setup(y =&gt; y.GetUserId("testName")).Returns(1);
  </code></pre>
  
  <p>Unfortunatelly this line of code gives me:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>If I remove the line above from my controller and use static value instead (int userId = 1) a test completes fine.</p>
  
  <p>Can anyone tell me why?</p>