title: Unit testing using Moq and setting up GetAll with include
body: <p>I have this strange problem with MOQ and I cant use the GetAll('include') method to test my controller.</p>
  
  <p>My Tests initialization</p>
  
  <pre><code>// GetAll
  menusDb.Setup(m =&gt; m.GetAll()).Returns(menus.AsQueryable());
  menusDb.Setup(m =&gt; m.GetAll(It.IsAny&lt;Expression&lt;Func&lt;Menu, object&gt;&gt;&gt;()))
         .Returns((Expression&lt;Func&lt;Menu,object&gt;&gt; pred) =&gt; { 
             return menus.AsQueryable();
         });
  // FindByIdAsync
  menusDb.Setup(m =&gt; m.FindByByIdAsync(It.IsAny&lt;int&gt;()))
         .Returns((int x) =&gt; Task.FromResult(menus.Find(m =&gt; m.ID == x)));
  menusDb.Setup(m =&gt; m.FindByByIdAsync(It.IsAny&lt;int&gt;(), It.IsAny&lt;Expression&lt;Func&lt;Menu, object&gt;&gt;[]&gt;()))
         .Returns((int x, Expression&lt;Func&lt;Menu,
             object&gt;&gt;[] includeProperties) =&gt; Task.FromResult(menus.Find(m =&gt; m.ID == x)));
  </code></pre>
  
  <p>Now whenever I am trying to test </p>
  
  <pre><code>_menusDB.GetAll(s=&gt;s.Sections)
  </code></pre>
  
  <p>The moq version of menusdb.getAll() method is not fired at all</p>
  
  <p>All other Moq methods are triggered correctly...
  examples </p>
  
  <pre><code>_menusDB.GetAll();
  _menusDB.FindByByIdAsync(id,
                  m =&gt; m.Sections.Select(s =&gt; s.Image),
                  m =&gt; m.Sections.Select(s =&gt; s.MenuItems.Select(mi =&gt; mi.Image)));
  </code></pre>
  
  <p>And these are the Getall and find functions with includes in my generic repository.</p>
  
  <pre><code>public IQueryable&lt;TEntity&gt; GetAll&lt;TProperty&gt;(Expression&lt;Func&lt;TEntity, TProperty&gt;&gt; propertyToInclude) {            
      return ObjectSet.Include(propertyToInclude);
  }
  
  public async Task&lt;TEntity&gt; FindByByIdAsync(int id, params Expression&lt;Func&lt;TEntity, object&gt;&gt;[] propertiesToInclude) {            
      var query = propertiesToInclude.Aggregate(ObjectSet as IQueryable&lt;TEntity&gt;, (current, property) =&gt; current.Include(property));
      return await query.SingleOrDefaultAsync(entity =&gt; entity.ID == id).ConfigureAwait(false);
  }
  </code></pre>