Last active
March 24, 2017 07:11
-
-
Save jpvelasco/8ed3e0d83749a55dcbee6240c8d71e03 to your computer and use it in GitHub Desktop.
Sample unit test using EF Core and SQL Lite In-Memory provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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