Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Created February 20, 2018 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebridge/a1188728a28f0f53b06fed791031c89d to your computer and use it in GitHub Desktop.
Save mikebridge/a1188728a28f0f53b06fed791031c89d to your computer and use it in GitHub Desktop.
Mock EF DbContext with SQLite for XUnit Test
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
// ...
public static MyDbContext InMemoryContext()
{
// SEE: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite
var connection = new SqliteConnection("Data Source=:memory:");
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
connection.Open();
// create the schema
using (var context = new MyDbContext(options))
{
context.Database.EnsureCreated();
}
return new MyDbContext(options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment