title: Moq with IQueryable Extension Methods
body: <p>I'm new to Moq Framework and Unit Testing in general, I'm trying to create a unit test for my repository function below.</p>
  
  <h3>Repository Method</h3>
  
  <pre><code>public IQueryable&lt;Campaign&gt; AllIncluding(params Expression&lt;Func&lt;Campaign, object&gt;&gt;[] includeProperties)
  {
      IQueryable&lt;Campaign&gt; query = _context.Campaigns;
      foreach (var includeProperty in includeProperties)
      {
          query = query.Include(includeProperty);
      }
      return query;
  }
  </code></pre>
  
  <h3>Unit Test with Moq Setup</h3>
  
  <pre><code>//Arrange
  var data = new List&lt;Models.Campaign&gt;
  {
      new Models.Campaign { Id = 1, Name = "Campaign Past", StartDate = DateTime.Now.AddDays(-10), EndDate = DateTime.Now.AddDays(-5) },
      new Models.Campaign { Id = 2, Name = "Campaign Active", StartDate = DateTime.Now.AddDays(-4), EndDate = DateTime.Now.AddDays(3) },
      new Models.Campaign { Id = 2, Name = "Campaign Future", StartDate = DateTime.Now.AddDays(4), EndDate = DateTime.Now.AddDays(10) }
  }.AsQueryable();
  var _moqSet = new Mock&lt;DbSet&lt;Models.Campaign&gt;&gt;();
  _moqSet.As&lt;IQueryable&lt;Models.Campaign&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
  _moqSet.As&lt;IQueryable&lt;Models.Campaign&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
  _moqSet.As&lt;IQueryable&lt;Models.Campaign&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
  _moqSet.As&lt;IQueryable&lt;Models.Campaign&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
  //_moqSet.As&lt;IQueryable&lt;Models.Campaign&gt;&gt;()
  //    .Setup(m =&gt; m.Include(It.IsAny&lt;Expression&lt;Func&lt;Models.Campaign, object&gt;&gt;&gt;()))
  //    .Returns((Expression&lt;Func&lt;Models.Campaign, object&gt;&gt; predicate) =&gt;
  //    {
  //        return _moqSet.Object.Include(predicate);
  //    });
  var _moqContext = new Mock&lt;Context.IPrizeSelectionContext&gt;();
  _moqContext.Setup(m =&gt; m.Campaigns).Returns(_moqSet.Object);
  Func&lt;IQueryable&lt;Models.Campaign&gt;, Expression&lt;Func&lt;Models.Campaign, object&gt;&gt;, IQueryable&lt;Models.Campaign&gt;&gt; includeMethod = (query, expression) =&gt;
  {
      return query.Include(expression);
  };
  //Act
  List&lt;Models.Campaign&gt; allResults = null;
  using (var sut = new CampaignRepository(_moqContext.Object, includeMethod))
  {
      allResults = sut.AllIncluding(o =&gt; o.Id, o =&gt; o.Name).OrderBy(o =&gt; o.Id).ToList();
  }
  //Assert
  Assert.IsNotNull(allResults);
  Assert.AreEqual(3, allResults.Count);
  Assert.IsNull(allResults[0].StartDate);
  </code></pre>
  
  <p>I'm getting a null data after the <code>query.include(includeProperty)</code> I've tried to mock the include function but I get an <code>Expression references a method that does not belong to the mocked object: m =&gt; m.Include&lt;Campaign,Object&gt;(It.IsAny&lt;Expression'1&gt;())</code> exception when trying to setup the Mock DbSet. After some digging I've found that Moq has issues or can't mock extension methods, see <a href="https://stackoverflow.com/questions/2295960/mocking-extension-methods-with-moq">Question Mocking Extionsion Methods with Moq</a></p>
  
  <p>Following the steps in <a href="http://adventuresdotnet.blogspot.ca/2011/03/mocking-static-methods-for-unit-testing.html" rel="nofollow noreferrer">This Blog</a> I've tried to execute the delegate pattern</p>
  
  <h3>Repository Re-write</h3>
  
  <pre><code>Func&lt;IQueryable&lt;Campaign&gt;, Expression&lt;Func&lt;Campaign, object&gt;&gt;, IQueryable&lt;Campaign&gt;&gt; _includeMethod = null;
  ...
  public CampaignRepository(IPrizeSelectionContext context, Func&lt;IQueryable&lt;Campaign&gt;, Expression&lt;Func&lt;Campaign, object&gt;&gt;, IQueryable&lt;Campaign&gt;&gt; includeMethod)
      : this(context)
  {
      _includeMethod = includeMethod;
  }
  ...
  public IQueryable&lt;Campaign&gt; AllIncluding(params Expression&lt;Func&lt;Campaign, object&gt;&gt;[] includeProperties)
  {
      IQueryable&lt;Campaign&gt; query = _context.Campaigns;
      foreach (var includeProperty in includeProperties)
      {
          if (_includeMethod == null)
              query = query.Include(includeProperty);
          else
              query = _includeMethod(query, includeProperty);
      }
      return query;
  }
  </code></pre>
  
  <p>Here is my Unit Test setup</p>
  
  <pre><code>Func&lt;IQueryable&lt;Models.Campaign&gt;, Expression&lt;Func&lt;Models.Campaign, object&gt;&gt;, IQueryable&lt;Models.Campaign&gt;&gt; includeMethod = (query, expression) =&gt;
  {
      return query.Include(expression);
  };
  
  List&lt;Models.Campaign&gt; allResults = null;
  using (var sut = new CampaignRepository(_moqContext.Object, includeMethod))
  {
      allResults = sut.AllIncluding(o =&gt; o.Id, o =&gt; o.Name).OrderBy(o =&gt; o.Id).ToList();
  }
  </code></pre>
  
  <p>However this pattern also ends the same way, the <code>IQueryable&lt;Campaign&gt; query = _context.Campaigns;</code> returns the Mock type but I cant use the extension method.</p>
  
  <p>Can anyone point me in the correct direction to test my repository method?</p>