// Creates and seeds an ApplicationContext with test data; then calls test action. private async Task PrepareTestContext(TestAction testAction) { // Database is in memory as long the connection is open var connection = new SqliteConnection("DataSource=:memory:"); try { connection.Open(); var options = new DbContextOptionsBuilder <IdentityContext>() .UseSqlite(connection) .Options; // Create the schema in the database and seeds with test data using (var context = new IdentityContext(options)) { context.Database.EnsureCreated(); SeedTestData.Initialize(context); await testAction(context); } } finally { connection.Close(); } }
public async Task OnGetAsyncPopulatesPageModel() { // Arrange: seed database with test data await PrepareTestContext(async(context) => { var expectedTechnician = SeedTestData.GetSeedingTechnicians()[0]; var expectedProject = SeedTestData.GetSeedingProjects()[0]; // Act: allocate tech to proj var TestedAllocate = new AllocateModel(context); await TestedAllocate.OnGetAsync(expectedProject.ProjectID); // Assert: employ correctly saved to db TestedAllocate.AllocateTechnician(expectedProject.ProjectID, expectedTechnician.TechnicianID); List <Employ> ActualEmploys = await context.Employ.AsNoTracking().ToListAsync(); Assert.True(ActualEmploys.Count > 0); Assert.Equal(ActualEmploys[0].TechnicianID, expectedTechnician.TechnicianID); Assert.Equal(ActualEmploys[0].ProjectID, expectedProject.ProjectID); }); }
public async Task OnGetAsyncPopulatesPageModel() { // Arrange: seed database with test data await PrepareTestContext(async(context) => { var expectedClients = SeedTestData.GetSeedingClients(); // Act: retrieve clients var pageModel = new IndexModel(context); await pageModel.OnGetAsync(); // Assert: seeded and retrieved clients match var actualMessages = Assert.IsAssignableFrom <List <Client> >(pageModel.Client); Assert.Equal( expectedClients.OrderBy(a => a.Name).Select(a => a.Name), actualMessages.OrderBy(a => a.Name).Select(a => a.Name)); }); }
public async Task ClientsAreRetrievedTest() { using (var db = new IdentityContext(Utilities.TestDbContextOptions())) { // Arrange: seed database with clients var expectedClients = SeedTestData.GetSeedingClients(); await db.AddRangeAsync(expectedClients); await db.SaveChangesAsync(); // Act: retrieve seeded clients from database var result = await db.Client.AsNoTracking().ToListAsync(); // Assert: seeded and retrieved clients match var actualClients = Assert.IsAssignableFrom <List <Client> >(result); Assert.Equal( expectedClients.OrderBy(a => a.Id).Select(a => a.Name), actualClients.OrderBy(a => a.Id).Select(a => a.Name)); } }