title: How do you stub IQueryable<T>.Where(Func<T, bool>) with Rhino Mocks?
body: <p>In the .net 3.5 project that I am working on right now, I was writing some tests for a service class. </p>
  
  <pre><code>public class ServiceClass : IServiceClass
  {
       private readonly IRepository _repository;
  
       public ServiceClass(IRepository repository)
       {
            _repository = repository;
       }
  
       #region IServiceClass Members
  
       public IEnumerable&lt;ActionType&gt; GetAvailableActions()
       {
           IQueryable&lt;ActionType&gt; actionTypeQuery = _repository.Query&lt;ActionType&gt;();
           return actionTypeQuery.Where(x =&gt; x.Name == "debug").AsEnumerable();
       }
  
       #endregion
  }
  </code></pre>
  
  <p>and I was having a hard time figuring out how to stub or mock the </p>
  
  <pre><code>actionTypeQuery.Where(x =&gt; x.Name == "debug")
  </code></pre>
  
  <p>part.</p>
  
  <p>Here's what I got so far:</p>
  
  <pre><code>[TestFixture]
  public class ServiceClassTester
  {
      private ServiceClass _service;
      private IRepository _repository;
      private IQueryable&lt;ActionType&gt; _actionQuery;
      [SetUp]
      public void SetUp()
      {
          _repository = MockRepository.GenerateMock&lt;IRepository&gt;();
          _service = new ServiceClass(_repository);
      }
  
      [Test]
      public void heres_a_test()
      {
          _actionQuery = MockRepository.GenerateStub&lt;IQueryable&lt;ActionType&gt;&gt;();
  
          _repository.Expect(x =&gt; x.Query&lt;ActionType&gt;()).Return(_actionQuery);
          _actionQuery.Expect(x =&gt; x.Where(y =&gt; y.Name == "debug")).Return(_actionQuery);
  
          _service.GetAvailableActions();
  
          _repository.VerifyAllExpectations();
          _actionQuery.VerifyAllExpectations();
      }
  
  }
  </code></pre>
  
  <p>[Note: class names have been changed to protect the innocent]</p>
  
  <p>But this fails with a <code>System.NullReferenceException</code> at </p>
  
  <pre><code>_actionQuery.Expect(x =&gt; x.Where(y =&gt; y.Name == "debug")).Return(_actionQuery);
  </code></pre>
  
  <p>So my question is: </p>
  
  <p>How do I mock or stub the IQueryable.Where function with RhinoMocks and get this test to pass?</p>
  
  <p>If my current setup won't allow me to mock or stub IQueryable, then give a reasoned explanation why.</p>
  
  <p>Thanks for reading this epically long question.</p>