Uses .Net Core v2.2
I'm not sure the best way to initalise the Jasper host in my unit tests. The problem I'm encountering is that my Db Context is not being correctly initialised with the Provider name unless I initialise the Jasper Host with a custom "TestStartup" class.
If I don't use a test specific
startup.cs class, and create a new service collection in my unit test, add the DbContext and call BuildServiceProvider(), any DbContext accessed from the Lamar container throws this error:
System.InvalidOperationException : No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
I've tried explicitly registering the DbContext in the Lamar Container, but no joy:
var opt = new DbContextOptionsBuilder<AppDbContext>() .UseSqlite(connection) .UseInternalServiceProvider(serviceProvider) .Options; registry.For<AppDbContext>().Use(t => new AppDbContext(opt));
I guess I'm looking for the best practice steps for this:
- Init Jasper Host
- Create Service Collection
- Build Service Provider
- Add DbContext (with options, incl service provider??)
- Start invoking messages on the command bus
Should I need to register my test DbContext in the container so that Jasper can get the right one when it invokes my command/query handlers?
Is it sufficent and suitable to provide a basic
TestStartup.cs class that calls