title: It.IsAny<T>() used as a mocked object
body: <p>Using moq, it is usual to provide a mock for an interface type <code>T</code> by using <code>Mock.Of&lt;T&gt;()</code>. But alternatively <code>It.IsAny&lt;T&gt;()</code> returns an Expression that also seems to be usable as an object in place of an object of type <code>T</code>. I've only seen <code>It.IsAny&lt;T&gt;()</code> used to define arguments for method calls in <code>Setup()</code>. What is the difference between <code>Mock.Of&lt;T&gt;()</code> and <code>It.IsAny&lt;T&gt;()</code> (besides syntax obviously)? Are there any circumstances where <code>It.IsAny&lt;T&gt;()</code> should be used to provide a mock over <code>Mock.Of&lt;T&gt;()</code>?</p>
  
  <p>Here is an example of what I'm asking, which I'll build on Ufuk's answer. The mock provided to <code>new UserService()</code> can be either <code>It.IsAny&lt;IUserRepository&gt;()</code> or <code>Mock.Of&lt;IUserRepository&gt;()</code>. In this case, I'm not interested in setting up anything special about these mocks, only that they exist to satisfy the compiler. The <code>Assert.IsTrue()</code> statements are agnostic to the supplied <code>IUserRepository</code> for the purposes of these tests. The question is: are <code>It.IsAny&lt;IUserRepository&gt;()</code> and <code>Mock.Of&lt;IUserRepository&gt;()</code> functionally equivalent in this instance?</p>
  
  <pre><code>[TestFixture]
  public class MoqTests
  {
      [Test] 
      public void TestInitializationWithItIsAny()
      {
          var subject = new UserService( It.IsAny&lt;IUserRepository&gt;() ); // It.IsAny&lt;T&gt;
          _userService.RegisterUser("abc");
          Assert.IsTrue( _userService.IsInitialized() );
      }
  
      [Test] 
      public void TestInitializationWithMockOf()
      {
          var subject = new UserService( Mock.Of&lt;IUserRepository&gt;() ); // Mock.Of&lt;T&gt;
          _userService.RegisterUser("abc");
          Assert.IsTrue( _userService.IsInitialized() );
      }
  }
  
  public class UserService
  {
      private readonly IUserRepository _userRepository;
      private bool _isInitialized = false;
  
      public UserService(IUserRepository userRepository)
      {
          _userRepository = userRepository;
      }
  
      public bool RegisterUser(string userName)
      {
          _isInitialized = true;
          User user = new User { UserName = userName, CreatedOn = DateTime.Now };
          return _userRepository.RegisterUser(user);
      }
  
      public bool IsInitialized()
      {
          return _isInitialized;
      }
  }
  
  public interface IUserRepository
  {
      bool RegisterUser(User user);
  }
  
  public class User
  {
      public string UserName { get; set; }
      public DateTime CreatedOn { get; set; }
  }
  </code></pre>