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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:51:39+00:00 2026-06-17T05:51:39+00:00

I have the following method in my repository public IQueryable<T> QueryWithInclude(String include) { return

  • 0

I have the following method in my repository

public IQueryable<T> QueryWithInclude(String include)
        {
            return _dbSet.Include(include);
        }

How can I mock such a method I am using Moq. I can’t seem to find out the correct way.

For e.g. I could do the following for

public IQueryable<T> Query()
            {
                return _dbSet;
            }

mockrepository.Setup(_ => _.Query()).Returns(foo.AsQueryable()); 

Also, if the mocking of such a method/s eventually turns out be difficult then is it wise to consider alternative implementations/ workarounds ?

  • 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-06-17T05:51:41+00:00Added an answer on June 17, 2026 at 5:51 am

    As long as your abstractions return IQueryable (like DbSet) then you can create your own Include extension to IQueryable. Your code will automaticalyl invoke your new Include extension method. When used on an abstraction that returns a DbQuery, it’ll invoke the correct Invoke, else if it’s a IQueryable instance it’ll do nothing. Adding this new extension method means that your current code should compile without any changes.

    like such:

        /// see: http://msdn.microsoft.com/en-us/library/ff714955.aspx
        ///     
        /// The method has been modified from version that appears in the referenced article to support DbContext in EF 4.1 ->
        /// 
        /// </summary>
        public static class ModelExtensions {
            public static IQueryable<T> Include<T>
                    (this IQueryable<T> sequence, string path) where T : class {
                var dbQuery = sequence as DbQuery<T>;
                if (dbQuery != null) {
                    return dbQuery.Include(path);
                }
                return sequence;
            }
        }
    

    So if your unit test uses a fake IQueryable list, the Include will do nothing.

    I must add though that there are strong opinions on why it is bad and worthless to mock out EntityFramework. If you haven’t see them it may be worth it checking them out.

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

Sidebar

Related Questions

I have the following method: public string Phase(string phase) { return Phase 1; }
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have the following action method that initiated SelectList as follow:- public PartialViewResult Search(string
i have the following action method that calls a repository method:- public ActionResult Details(int
I have written the following method using the Repository mentioned in the following blog-post
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I thought this was fixed in 4.0. I have this method public IQueryable<T> All(Expression<Func<T,object>>
I have a service containing the following method, which queries a repository, like so:

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.