-
Notifications
You must be signed in to change notification settings - Fork 1
Associated with EntityFrameworkCore
NightAngell edited this page Feb 23, 2019
·
9 revisions
Knowledge from basics is required to understand this topic.
You can use this features both with
HubUnitTestsWithEF<TDbContext>
andHubUnitTestsWithEF<T, TDbContext>
//By default, pure TDbContext mock, without any setup.
public Mock<TDbContext> DbContextMock;
For example we have Hub which is dependend from DbContext. However We testing method which not use that DbContext. So better not initialize entire in memory database (remember, My In memory databases are lazy loaded) when we dont need it and instead use mock (which is also lazy loaded).
//Relational database in memory.
//During instance creation it ensure if db is created.
//https://docs.microsoft.com/pl-pl/ef/core/miscellaneous/testing/sqlite
public TDbContext DbInMemorySqlite;
//Database in memory (not really relational).
//During instance creation it ensure if db is created.
//https://docs.microsoft.com/pl-pl/ef/core/miscellaneous/testing/in-memory
public TDbContext DbInMemory;
public void TestingMethod_TestScenario_ExpectedResult()
{
//Arrange
//Let's assume we have table with notes
var note = new Note { Title="Lorem", Content="Ipsum"};
DbInMemorySqlite.Notes.Add(note);
DbInMemorySqlite.SaveChanges();
...
//Act
//Assert
}