Integration Testing Entity Framework code first with in-memory database

后端 未结 2 494
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 02:09

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, agains

相关标签:
2条回答
  • 2020-12-03 02:39

    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" />
    
    0 讨论(0)
  • 2020-12-03 02:59

    Take a look at this article: Faking your LINQ provider. It describes how you can hide Entity Framework behind an abstraction, so that you can easily unit test your application, while still allowing to use LINQ (over IQueryable) queries against it in your application code.

    Note that this does not completely remove the need of writing integration tests, since you would still need to test the database mapping layer and possibly test whether the chosen LINQ provider is able to execute the LINQ queries.

    0 讨论(0)
提交回复
热议问题