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 116137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:07:49+00:00 2026-05-11T03:07:49+00:00

I am using NHibernate and Rhinomocks and having trouble testing what I want. I

  • 0

I am using NHibernate and Rhinomocks and having trouble testing what I want. I would like to test the following repository method without hitting the database (where _session is injected into the repository as ISession):

public class Repository : IRepository {     (... code snipped for brevity ...)      public T FindBy<T>(Expression<Func<T, bool>> where)     {           return _session.Linq<T>().Where(where).FirstOrDefault();     } } 

My initial approach is to mock ISession, and return an IQueryable stub (hand coded) when Linq is called. I have a IList of Customer objects I would like to query in memeory to test my Linq query code without hitting the db. And I’m not sure what this would look like. Do I write my own implementation of IQueryable? If so, has someone done this for this approach? Or do I need to look at other avenues?

Thanks!

  • 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. 2026-05-11T03:07:50+00:00Added an answer on May 11, 2026 at 3:07 am

    How I’ve done this test is to not pass the expression to the repository, instead expose IQueryable giving the repository an interface such as:

    public interface IRepository<T> {     IQueryable<T> All();     // whatever else you want } 

    Easily implemented like so:

    public IQueryable<T> All() {     return session.Linq<T>(); } 

    This means that instead of calling your method on the repository like:

    var result = repository.FindBy(x => x.Id == 1); 

    You can do:

    var result = repository.All().Where(x => x.Id == 1); 

    Or the LINQ syntax:

    var result = from instance in repository.All()              where instance.Id == 1              select instance; 

    This then means you can get the same test by mocking the repository out directly which should be easier. You just get the mock to return a list you have created and called AsQueryable() on.

    As you have pointed out, the point of this is to let you test the logic of your queries without involving the database which would slow them down dramatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using nHibernate, I would like to query on an integer datatype, but its always
I'm using NHibernate 2.1.2, and would like to load a many-to-one association using a
I'm using NHibernate 2, and I'd like to be able to do the following:
I am using nhibernate to map the following classes: public class DeviceConfig : EntityBase
I'm using nhibernate as my ORM and Firebird embedded as the database. How would
I am using NHibernate with fluent mapping. I have the following scenario: public class
I'm using NHibernate for a web application. I have a model like this: public
I'm using NHibernate with RIA services the following association is causing me problems. User
Using NHibernate, how can I map the following scenario: public class User { public
We are using NHibernate with SQL Server for our backend. I want to retrieve

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.