title: Mocking using MOQ and generic types
body: <p>Method to unit test: GetUserInfo</p>
  
  <p>Following is the class containing the method: </p>
  
  <pre><code>public class AccountService : IAccountService
  {
      IUnitOfWork _UnitOfWork;
  
      public AccountService(IUnitOfWork unitOfWork)
      {
          _UnitOfWork = unitOfWork;
      }
  
      public UserInfo GetUserInfo(string userName, string password)
      {
          var userInfo = new UserInfo();
          userInfo.UserType = UserType.Invalid;
  
     // Statement of interest
     var portalUser = _UnitOfWork.Repository&lt;DvaPortalUser&gt;().Query().Filter(t =&gt; t.Email == userName &amp;&amp; t.Password == password).Get().FirstOrDefault();
  
          //....Rest of the code is not included for clarity
      }
  }
  </code></pre>
  
  <p>The interface to mock IUnitOfWork:</p>
  
  <pre><code>public interface IUnitOfWork
  {
      void Dispose();
      void Save();
      void Dispose(bool disposing);
      IRepository&lt;T&gt; Repository&lt;T&gt;() where T : class;
  }
  </code></pre>
  
  <p>Repository Implementation:</p>
  
  <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
  
      public virtual RepositoryQuery&lt;TEntity&gt; Query()
      {
          var repositoryGetFluentHelper = new RepositoryQuery&lt;TEntity&gt;(this);
  
          return repositoryGetFluentHelper;
      }
  
    internal IEnumerable&lt;TEntity&gt; Get(
          Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
          Func&lt;IQueryable&lt;TEntity&gt;,
              IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
          List&lt;Expression&lt;Func&lt;TEntity, object&gt;&gt;&gt;
              includeProperties = null,
          int? page = null,
          int? pageSize = null)
      {
          IQueryable&lt;TEntity&gt; query = DbSet;
  
          if (includeProperties != null)
              includeProperties.ForEach(i =&gt; query.Include(i));
  
          if (filter != null)
              query = query.Where(filter);
  
          if (orderBy != null)
              query = orderBy(query);
  
          if (page != null &amp;&amp; pageSize != null)
              query = query
                  .Skip((page.Value - 1)*pageSize.Value)
                  .Take(pageSize.Value);
  
  
          return query.ToList();
      }
  }
  </code></pre>
  
  <p>RepositoryQuery Implementation:</p>
  
  <pre><code>public sealed class RepositoryQuery&lt;TEntity&gt; where TEntity : class
  {
      private readonly List&lt;Expression&lt;Func&lt;TEntity, object&gt;&gt;&gt; _includeProperties;
  
      private readonly Repository&lt;TEntity&gt; _repository;
      private Expression&lt;Func&lt;TEntity, bool&gt;&gt; _filter;
      private Func&lt;IQueryable&lt;TEntity&gt;,
          IOrderedQueryable&lt;TEntity&gt;&gt; _orderByQuerable;
      private int? _page;
      private int? _pageSize;
  
      public RepositoryQuery(Repository&lt;TEntity&gt; repository)
      {
          _repository = repository;
          _includeProperties = new List&lt;Expression&lt;Func&lt;TEntity, object&gt;&gt;&gt;();
      }
  
      public RepositoryQuery&lt;TEntity&gt; Filter(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter)
      {
          _filter = filter;
          return this;
      }
  
  
      public IEnumerable&lt;TEntity&gt; Get()
      {
          return _repository.Get(
              _filter,
              _orderByQuerable, _includeProperties, _page, _pageSize);
      }
  }
  </code></pre>
  
  <p>Unit Test Method:</p>
  
  <pre><code> [TestMethod]
   public void AccountService_GetUserInfo_SuccessfulLogin()
   {
      var  _UnitOfWork = new Mock&lt;IUnitOfWork&gt;();
      _AccountService = new AccountService(_UnitOfWork.Object);
  
      _UnitOfWork.Setup(a =&gt;  a.Repository&lt;T&gt;())).Returns(??); //How do I setup this statement?
      _UnitOfWork.VerifyAll();
   }
  </code></pre>
  
  <p>Question: How do I setup mock call for the statement _UnitOfWork.Repository()?</p>