Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration Testing Entity Framework code first with in-memory database

I'd like to run actual integration tests of my EF4.1 repositories against an in-memory database a la ayende's nhibernate version.

I have a code first model, against a legacy database (old table and column names need mapping to my entites using code configurations).

I'd like to be able to use Sqlite (or other) to:

  1. Generate an in-memory database from my model
  2. Create a DBContext for my model with this in-memory database
  3. I have already in place IoC/DI of a IDBContextFactory which gets constructed with my (Generic) Repositories (also using a GenericRepository pattern)

There's bits and bobs on-line which suggest it should be possible, but not much for code-first approaches. Anyone know if this is possible?

Some snippets of my test library, see // THROWS ERROR marking runtime errors:

public class MyDbContextFactory : IDbContextFactory
  {
    private static object context;
    public object CurrentContext
    {
      get {
        if(context == null)
        {
          // ?? DOESN'T WORK AS THERE'S NO META DATA
          var connBuilder = new EntityConnectionStringBuilder();
          connBuilder.Provider = "System.Data.SQLite";
          connBuilder.Metadata = 
           @"res://*/TestEfDb.csdl|res://*/TestEfDb.ssdl|res://*/TestEfDb.msl";
          connBuilder.ProviderConnectionString = 
           ConfigurationManager.ConnectionStrings["DataContext"].Name;

          var entConnection = new EntityConnection(connBuilder.ConnectionString);

          // THROWS ERROR: sqlite Format of the initialization string does not
          // conform to specification starting at index 0
          // for connection string "Data Source=:memory:;Version=3;New=True;"

          //var entConnection = new EntityConnection
          // (ConfigurationManager.ConnectionStrings["DataContext"].Name);
          context = new MyDbContext(entConnection);
        }
        return context;
      }
    }
  }

...

 [Test]
    public void test_me()
    {
        var auditRespository = new AuditRepository(new MyDbContextFactory());
        auditRespository.GetAll<Audit>();
    }
like image 535
jenson-button-event Avatar asked May 06 '11 13:05

jenson-button-event


People also ask

How is integration testing applied on code?

Integration testing is performed using the black box method. This method implies that a testing team interacts with an app and its units via the user interface – by clicking on buttons and links, scrolling, swiping, etc. They don't need to know how code works or consider the backend part of the components.

What is integration test in database?

Integration testing is done to test the modules/components when integrated to verify that they work as expected i.e. to test the modules which are working fine individually does not have issues when integrated.

What is in-memory Entity Framework?

EF Core In-Memory Database Provider This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases.


1 Answers

Use SQL Compact 4.0 (download both SqlCE and tools by web platform installer) - EF Code first has direct support for that. The only difference will be that your application will use connection string to big SQL Server:

<add name="MyDbContext" 
     provider="System.Data.SqlClient" 
     connectionString=
       "Data Source=...;InitialCatalog=...;Integrated Security=SSPI" />

and your tests will use connection string to SQL Compact:

<add name="MyDbContext" 
     provider="System.Data.SqlServerCe.4.0" 
     connectionString="Data Source=Database.sdf" />
like image 197
Ladislav Mrnka Avatar answered Oct 02 '22 11:10

Ladislav Mrnka