Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 208565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:50:13+00:00 2026-05-11T17:50:13+00:00

In the .net 3.5 project that I am working on right now, I was

  • 0

In the .net 3.5 project that I am working on right now, I was writing some tests for a service class.

public class ServiceClass : IServiceClass
{
     private readonly IRepository _repository;

     public ServiceClass(IRepository repository)
     {
          _repository = repository;
     }

     #region IServiceClass Members

     public IEnumerable<ActionType> GetAvailableActions()
     {
         IQueryable<ActionType> actionTypeQuery = _repository.Query<ActionType>();
         return actionTypeQuery.Where(x => x.Name == "debug").AsEnumerable();
     }

     #endregion
}

and I was having a hard time figuring out how to stub or mock the

actionTypeQuery.Where(x => x.Name == "debug")

part.

Here’s what I got so far:

[TestFixture]
public class ServiceClassTester
{
    private ServiceClass _service;
    private IRepository _repository;
    private IQueryable<ActionType> _actionQuery;
    [SetUp]
    public void SetUp()
    {
        _repository = MockRepository.GenerateMock<IRepository>();
        _service = new ServiceClass(_repository);
    }

    [Test]
    public void heres_a_test()
    {
        _actionQuery = MockRepository.GenerateStub<IQueryable<ActionType>>();

        _repository.Expect(x => x.Query<ActionType>()).Return(_actionQuery);
        _actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);

        _service.GetAvailableActions();

        _repository.VerifyAllExpectations();
        _actionQuery.VerifyAllExpectations();
    }

}

[Note: class names have been changed to protect the innocent]

But this fails with a System.NullReferenceException at

_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);

So my question is:

How do I mock or stub the IQueryable.Where function with RhinoMocks and get this test to pass?

If my current setup won’t allow me to mock or stub IQueryable, then give a reasoned explanation why.

Thanks for reading this epically long question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:50:13+00:00Added an answer on May 11, 2026 at 5:50 pm

    I initially wanted to mock out calls like ‘IQueryable.Where(Func)’ too but I think it is testing at the wrong level. Instead in my tests I just mocked out IQueryable and then check the result:

    // Setup a dummy list that will be filtered, queried, etc
    var actionList = new List<ActionType>()
    {
        new ActionType() { Name = "debug" },
        new ActionType() { Name = "other" }
    };
    _repository.Expect(x => x.Query<ActionType>()).Return(actionList);
    
    var result = _service.GetAvailableActions().ToList();
    
    // Check the logic of GetAvailableActions returns the correct subset 
    // of actionList, etc:
    Assert.That(result.Length, Is.EqualTo(1));
    Assert.That(result[0], Is.EqualTo(actionList[0]);
    
    _repository.VerifyAllExpectations();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large project that uses Oracle ODP.NET. Right now it is
I am working on a project right now that involves receiving a message from
I'm currently working on an C#/ASP.NET project that will host several differents e-commerce websites,
I'm working on asp.net (vb, maybe that can change something) I have a project
I've got this ASP.NET 2.0 website project in Visual Studio that I've been working
I have a vb.net project that loads plugin dlls at runtime to crunch some
Right now, I am working on a web project in PHP and for the
I'm working on project for old PDAs that run on the .NET 2.0 CF
I'm currently writing some general purpose .net libraries that contain usual helper classes. For
On this project I am working on right now, one of the newest feature

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.