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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:54:06+00:00 2026-06-07T20:54:06+00:00

I have a database repository class: public class DatabaseRepository : IRepository { private readonly

  • 0

I have a database repository class:

public class DatabaseRepository : IRepository
    {
        private readonly Database _database;

        public DatabaseRepository(Database database)
        {
            _database = database;
        }

        ...

        public void Delete<TObject>(TObject entity) where TObject : BaseEntity
        {
            var dbSet = DbSet<TObject>();
            dbSet.Remove(entity);
            Save();
        }

        ...

        private void Save()
        {
            try
            {
                 _database.SaveChanges();
            }
            catch(DbEntityValidationException dbEx)
            {
                // do some action
            }
        }    
    }

I need to test catch block of private void Save() method, so I need something like…

[TestMethod]
public void SaveShouldDoSomethingIfDabaBaseConnectFalls()
{
    Mock<Database> mockDb = new Mock<Database>();
    mockDb.Setup(db => db.SaveChange()).Throws(new DbEntityValidationException());

    IRepository repository = new DatabaseRepository(mockDb.Object);

    ...
}

So my question is how can I mock entity framework Database? Mock<Database> mockDb = new Mock<Database>(); – how to write it correctly???? For mocking I use Moq.

  • 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-07T20:54:07+00:00Added an answer on June 7, 2026 at 8:54 pm
    1. Define IDatabase interface for your DbContext with set of typified IIDbSet<T> properties
    2. Implement FakeDbSet<T>: IDbSet<T>
    3. Update Repository to use IDatabase instead of Database

    After that you can construct mock of IDatabase.

    http://refactorthis.wordpress.com/2011/05/31/mock-faking-dbcontext-in-entity-framework-4-1-with-a-generic-repository/

    With doing that remember that unit test implemeted against fake db set does not guarantee that your code will work against real database.

    LINQ-to-Entities (real DbSet) does not support many features that LINQ-to-Objects (FakeDbSet) does. So your code can fail in execution-time when you work with database.

    This restriction does not make unit tests implemented against faked DbSet useless. Test still can test business rules implemented in your repository. But you need to run every updated linq statement (manually or from integration test) against real database LINQ-to-Entities db provider to make sure that provider support it.

    There are more info regarding this “holy war” topic here – Fake DbContext of Entity Framework 4.1 to Test

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

Sidebar

Related Questions

Suppose we have typical repository public class Repository:IRepository<Entity> { public Entity GetById(int id) {
If i have the following IQueryable<T> Repository: public class OrderRepository : GenericRepository<Order>, IOrderRepository {
I have a NHibernate repository that looks like this: public class NHibRepository : IDisposable
I have created a repository that is returning data from my database using Entity
I have entity fluently mapped to existing oracle view: public class MyEntityMap : ClassMap<MyEntity>
I have a database repository with a bunch of access functions. I now want
I have a generic DAO class that looks like this: public class GenericDaoJpa <T
Let just say I have the following data models: public class Account { public
I have created a simple repository class that is returning a list of Projects:
I have a repository class that uses an NHibernate session to persist objects to

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.