Skip to content

Instantly share code, notes, and snippets.

@jpvelasco
Last active March 24, 2017 07:11
Show Gist options
  • Save jpvelasco/8ed3e0d83749a55dcbee6240c8d71e03 to your computer and use it in GitHub Desktop.
Save jpvelasco/8ed3e0d83749a55dcbee6240c8d71e03 to your computer and use it in GitHub Desktop.
Sample unit test using EF Core and SQL Lite In-Memory provider
[Fact]
public void GetClients_WhenCalledWithSqlLiteInMemoryDataContext_ReturnsExpectedResult()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var sqlLiteDataContextOptions = new DbContextOptionsBuilder<EventDataContext>()
.UseSqlite(connection)
.Options;
// This will update the database with the current schema for the test
using (var context = new EventDataContext(sqlLiteDataContextOptions))
{
context.Database.EnsureCreated();
}
// Need to add a record with the given data context configuration,
// in this case, SQL Lite.
CreateTestClient(sqlLiteDataContextOptions);
using (var eventDataContext = new EventDataContext(sqlLiteDataContextOptions))
{
// Need to inject a separate data context into the controller,
// then the results will be asserted against the current records
var controller = new ClientsController(eventDataContext);
OkObjectResult okResult = Assert.IsType<OkObjectResult>(controller.GetClients());
List<Client> returnSession = Assert.IsType<List<Client>>(okResult.Value);
Assert.True(returnSession.FirstOrDefault().LastName == "last");
}
}
finally
{
connection.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment