Skip to content

Instantly share code, notes, and snippets.

@jpvelasco
Last active March 24, 2017 07:11
Show Gist options
  • Save jpvelasco/31e3a25811907d5cc6633ba18a3ce40d to your computer and use it in GitHub Desktop.
Save jpvelasco/31e3a25811907d5cc6633ba18a3ce40d to your computer and use it in GitHub Desktop.
Sample test code using EF Core In-Memory provider.
[Fact]
public void GetClients_WhenCalledWithInMemoryDataContext_ReturnsExpectedResult()
{
var inMemoryDataContextOptions = new DbContextOptionsBuilder<EventDataContext>()
.UseInMemoryDatabase(databaseName: "Test_With_In_Memory_Database")
.Options;
// NOTE: Because we will need to assert against known data,
// we need to seed the in-memory test database
// with the same context options as the unit test
CreateTestClient(inMemoryDataContextOptions);
var eventDataContext = new EventDataContext(inMemoryDataContextOptions);
var controller = new ClientsController(eventDataContext);
// NOTE: As a side note, in a real world test you will ideally have a specific
// Assert per test, but for sample purposes multiple asserts are used here.
OkObjectResult okResult = Assert.IsType<OkObjectResult>(controller.GetClients());
List<Client> returnSession = Assert.IsType<List<Client>>(okResult.Value);
Assert.True(returnSession.FirstOrDefault().FirstName == "first");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment