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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:11+00:00 2026-05-26T22:56:11+00:00

I am using Entity Framework 4.1 for database access and would like to Unit

  • 0

I am using Entity Framework 4.1 for database access and would like to Unit Test the following code:

// Get all the entities including children
using (MyContext context = new MyContext())
{
    return context.EmployeeProfiles.Include("EmployeeProperties").ToList();
}

I am using Moles to mole out the database dependency however I am stuck. Which point in the Entity Framework I should begin to mole out.

I was following this example but it is for LINQ-To-SQL.

I was also thinking of debugging/tracing the Entity Framework to figure out which function to intercept out before the call to the database is made.
However, it seems that there is no source code available for Entity Framework 4.1 to trace with. See discussion.

Can anyone guide me to which function(s) I should be moling out in the DbContext so I can get a list of EmployeeProfiles back?

  • 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-26T22:56:11+00:00Added an answer on May 26, 2026 at 10:56 pm

    It looks to me like rather than removing the dependency on EF via a Repository pattern, you’re trying to mock the specific behaviour of EF. I can’t see the point in trying to do that and it would be very difficult, EF isn’t meant to be mocked.

    Your repository is presumably something like this:

    public interface IRepository
    {
          IEnumerable<EmployeeProfiles> EmployeeProfiles { get; }
    }
    
    public class Repository
    {
        public IEnumerable<EmployeeProfiles> EmployeeProfiles
        {
            get
            {
                // Get all the entities including children     
                using (MyContext context = new MyContext())     
                {
                    return context.EmployeeProfiles.Include("EmployeeProperties").ToList();     
                }
            }
        }
    }
    

    This way you have removed the repository dependency on how the EmployeeProfiles is returned. Now you can mock-away to your heart’s content (haven’t yet used Moles), but with Moq, you would do something like:

    public void TestEmptyList()
    {
        var mock = new Mock<IRepository>();
        var expected = new List<EmployeeProfiles>();
        mock.SetupGet(ep => ep.EmployeeProfiles).Returns(expected);
    
        var actual = mock.Object.EmployeeProfiles;
    
        Assert.AreEqual(expected, actual);
    }
    

    So, if you put the methods/properties you want to abstract away from the database into the repository interface, then you can mock out any values you want to test that it might return.

    Maybe you’re doing this anyway, I’m not sure. I don’t see why you’d want to unit test EF, what would you hope to gain? It would be immensely hard, it’s not designed to be mocked (very few interfaces/ virtuals). Any mocking of data you return, which is really all you are really interesting in would be done as above.

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

Sidebar

Related Questions

I'm using Entity Framework to access my MySQL database. The model was generated using
I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF).
We are using entity framework 4 to access our database. Every table has got
I'm tring to insert some records into my Sqlite database using the Entity Framework.
I work with Entity Framework - add records in the database using classes that
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
I'm using Entity Framework for creation of my Data Access Layer and I want
We've got a database with over 1000+ tables and would like to consider using
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
We are using .Net Entity Framework to do our database related work. Our database

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.