title: Unit testing with queries defined in extension methods
body: <p>In my project I am using the following approach to querying data from the database:</p>
  
  <ol>
  <li>Use a generic repository that can return any type and is not bound to one type, i.e. <code>IRepository.Get&lt;T&gt;</code> instead of <code>IRepository&lt;T&gt;.Get</code>. NHibernates <code>ISession</code> is an example of such a repository.</li>
  <li><p>Use extension methods on <code>IQueryable&lt;T&gt;</code> with a specific <code>T</code> to encapsulate recurring queries, e.g.</p>
  
  <pre><code>public static IQueryable&lt;Invoice&gt; ByInvoiceType(this IQueryable&lt;Invoice&gt; q,
                                                  InvoiceType invoiceType)
  {
      return q.Where(x =&gt; x.InvoiceType == invoiceType);
  }
  </code></pre></li>
  </ol>
  
  <p>Usage would be like this:</p>
  
  <pre><code>var result = session.Query&lt;Invoice&gt;().ByInvoiceType(InvoiceType.NormalInvoice);
  </code></pre>
  
  <p>Now assume I have a public method I want to test that uses this query. I want to test the three possible cases:</p>
  
  <ol>
  <li>The query returns 0 invoices</li>
  <li>The query returns 1 invoice</li>
  <li>The query returns multiple invoices</li>
  </ol>
  
  <p><strong>My problem now is: What to mock?</strong></p>
  
  <ul>
  <li>I can't mock <code>ByInvoiceType</code> because it is an extension method, or can I?</li>
  <li>I can't even mock <code>Query</code> for the same reason.</li>
  </ul>