public void Add_writes_to_database() { // All contexts that share the same service provider will share the same InMemory database var options = CreateNewContextOptions(); // Run the test against one instance of the context using (var context = new BloggingContext(options)) { var service = new BlogService(context); service.Add("http://sample.com"); } // User a seperate instance of the context to verify correct data was saved to database using (var context = new BloggingContext(options)) { Assert.AreEqual(1, context.Blogs.Count()); Assert.AreEqual("http://sample.com", context.Blogs.Single().Url); } }
public void Add_writes_to_database() { // All contexts created from this service provider will access the same InMemory database var serviceProvider = _serviceCollection.BuildServiceProvider(); // Run the test against one instance of the context using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope()) { var context = scope.ServiceProvider.GetService<BloggingContext>(); var service = new BlogService(context); service.Add("http://sample.com"); } // User a seperate instance of the context to verify correct data was saved to database using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope()) { var context = scope.ServiceProvider.GetService<BloggingContext>(); Assert.AreEqual(1, context.Blogs.Count()); Assert.AreEqual("http://sample.com", context.Blogs.Single().Url); } }