title: Mocking FormsIdentity.Ticket.UserData with Moq
body: <p>As part of a unit test I am trying to mock the return value of FormsIdentity.Ticket.UserData</p>
  
  <p>The following will NOT work but it should give an idea of what I am trying to do:</p>
  
  <pre><code>var principal = Mock&lt;IPrincipal&gt;();
  var formsIdentity = Mock&lt;FormsIdentity&gt;();
  formsIdentity.Setup(a =&gt; a.Ticket.UserData).Returns("aaa | bbb | ccc");
  principal.Setup(b =&gt; b.Identity).Returns(formsIdentity.Object);
  </code></pre>
  
  <p>The code I am trying to test looks something like this:</p>
  
  <pre><code>FormsIdentity fIdentity = HttpContext.Current.User.Identity as FormsIdentity;
  string userData = fIdentity.Ticket.UserData;
  </code></pre>
  
  <p>All I want to do in my unit test is fake the return value from FormsIdentity.Ticket.UserData. But when I run the code in the first section I get an error when trying to mock the FormsIdentity. The error says the type to mock must be an interface, abstract class or non-sealed class.</p>
  
  <p>I tried to use IIdentity instead of FormsIdentity (FormsIdentity is an implementation of IIdentity) but IIdentity doesn't have .Ticket.UserData.</p>
  
  <p>So how can I write this test so that I get a value from FormsIdentity.Ticket.UserData?</p>