2

I'm using Entity Framework 4, Ninject, and Moq throughout my projects. When I get to meat of my service classes where EF4 is used, I instantiate a data context - this is bad.

I want to inject the data context into my services so my unit tests can work against a mock data context. I've done this before with nHibernate but I can't figure it out with EF4 and I can't find a solid example online.

Can anyone point me to a working solution and examples? Thanks much!

znelson
  • 919
  • 1
  • 10
  • 24

2 Answers2

4

The recommended approach is to use an in memory database to test against (using SQLite), similar to the technique for testing NHibernate as described in the NHibernate 3.0 cookbook.

You can also have a look here for a description of creating a fake DbContext.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • This is probably the best example I've found, but it skips over how to integrate code that actually touches a database and instead just talks about how to mock everything up: http://blogs.msdn.com/b/adonet/archive/2009/12/17/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspx I was hoping to find a working example of an EF4 project that touches the db, and then how to modify it for DI, mocking/testing, etc. – znelson Mar 09 '12 at 20:32
  • 2
    That example uses a repository pattern over EF, which would allow you to mock the repository implementation. However, there are good arguments for not using a repository with an ORM. See http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point/5626884#5626884. Also, see http://stackoverflow.com/questions/5609508/asp-net-mvc3-and-entity-framework-code-first-architecture/5610685#5610685 for a discussion on the use of integration tests. – devdigital Mar 09 '12 at 20:42
  • One of the arguments is that if you use a repository and mock the implementation, then you are not testing Entity Framework, and your LINQ queries could fail in production, so integration tests with in memory databases are actually testing EF. – devdigital Mar 09 '12 at 20:44
0

In my company we are using the local development database with a TransactionScope to roll back the changes. Unique side effect is increasing the identity when testing inserts, but this is not a problem.

The advantage is that you have a real test against the database.

If you need to send e-mails or send data to another server, then go back to the IoC with dependency injection, using ninject/moq.

You will need to add the Entity Framework and the connection string into your test project.

[TestClass]
public class NameValueTest
{
    [TestMethod]
    public void Create()
    {
        NameValueController controller = new NameValueController();

        using (var ts = new TransactionScope())
        {
            Assert.IsNotNull(controller.Create(new Models.NameValue()
            {
                name1 = "1",
                name2 = "2",
                name3 = "3",
                name4 = "4"
            }));

            //no complete, automatically abort
            //ts.Complete();
        }
    }
}

I hope this could help you.

Marquinho Peli
  • 4,795
  • 4
  • 24
  • 22