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:
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>();
}
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.
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With