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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:30:19+00:00 2026-05-30T09:30:19+00:00

I have an application that contains methods that work with data using Entity Framework

  • 0

I have an application that contains methods that work with data using Entity Framework 4.2 Code First and a MySQL database. I am trying to figure out a good way to write an MSTest unit test for these methods. For example:

DataModel:

public class User
{
    public User() { }
    [Key]
    public int UserID { get; set; }
    public string Role { get; set; }
}

public class AppDbContext : DbContext 
{
    public DbSet<User> Users { get; set; }
}

Business Layer:

public class Bus
{
    public bool UserIsInRole(int userID, string role)
    {
        using(var context = new AppDbContext()) 
        {
            User user = context.Users.SingleOrDefault(p => p.UserID == userID);
            if (user == null)
                return false;
            return user.Roles.Split(',').Contains(role);
        }
    }
}

I am trying to write a set of unit tests for the UserIsInRole function, but I want to try to isolate myself from actually having to read and write to the actual database since I cannot guarantee its state before the test. Setting up/tearing down a database just for this test would take too long.

I have ran into many articles about using fake DbContext, such as here, here, and here but they all seem to have some pros and cons. One group of people say that one should not write unit tests against EF and that this belongs to integration testing and that any fake DbContext don’t behave enough like the real thing for the purpose of acceptable tests.

I think code like this lies someplace in the middle of the argument. Ideally, I want to create a set of temporary, in-memory objects that represent the desired data without having to actually store it into the database.

How would you change the above and write a set of tests that verify that the UserIsInRole method:

  1. returns false if the userID does not exist in the Users
    collection.
  2. returns false if the user does not contain the
    desired role.
  3. returns true if the user has the desired role.

Keep in mind that this is a simplified example and that code could actually contain multiple queries of arbitrary complexity so I am hoping to find something a bit more comprehensive than, say, moving every query to a virtual function that is replaced by the test framework to return a predefined User record.

  • 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-30T09:30:20+00:00Added an answer on May 30, 2026 at 9:30 am

    I would seperate the knowledge of EF from the rest of your domain. If you disect DbSet you’ll find that it implements IQueryable which is enough for EF to work. Create an interface that defines your domain context and make your different concrete implementations (EF and Fake) implement that interface like:

    public class User 
    { 
        public User() { } 
        [Key] 
        public int UserID { get; set; } 
        public string Role { get; set; } 
    } 
    
    public interface IAppDomain
    {
        public IQueryable<User> Users { get; }
    }
    
    public class AppDbContext : DbContext, IAppDomain
    { 
        // exposure for EF
        public DbSet<User> Users { get; set; } 
    
        IAppDomain.IQueryable<User> Users { get { return ((AppDbContext)this).Users; }
    } 
    
    public class FakeAppDomain : IAppDomain
    {
        private List<User> _sampleUsers = new List<User>(){
            new User() { UserID = 1, Role = "test" }
        }
    
        public IQueryable<User> Users { get { return _sampleUsers; } }
    }
    

    This can be used in ways like:

    IQueryable<User> GetUsersByManagerRole(IAppDomain domain)
    {
        return from u in domain.Users
               where u.Role == "Manager"
               select u;
    }
    

    This allows you to create a fake implementation that takes any type of sample input. Next in your unit test, you create a new FakeDomainContext in which you set the state in the desired way for your unit test. Want to test of users with a certain role can be found? Create a FakeDomainContext with users with some test roles and try to find them. Easy and clean.

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

Sidebar

Related Questions

I have my main application delegate which contains a method that returns an object.
I have an application that contains many controls on a panel, each with its
I have an application that contains a VC++ project (along with C# projects). Previously,
We have a windows application that contains an ActiveX WebBrowser control. As part of
I have a console application that contains quite a lot of threads. There are
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have this senario. We have an application server that contains a few web
I have a web application (.war) that contains some static files (e.g. MS word
I have a C# Windows Form application that contains a menu with this event:
I have a scroll viewer in my application that contains a canvas, in which

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.