How to mock EntityFramework Database created by codefirst strategy?

c# entity-framework mocking unit-testing

Question

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
7/13/2012 1:31:38 AM

Accepted Answer

  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

4
5/23/2017 12:17:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow