How can I perform migrations using my test database context implementation?

45 Views Asked by At

I'm creating integration tests against an API using a dockerised database. Normally I'd resolve the database context and run context.Database.MigrateAsync() to set up the database before each test executed. An example of that is here.

In another test I want to intercept the call to SaveChangesAsync to verify what happens when there are concurrent updates to the database. The API depends on the real SampleDbContext, whereas the test injects a fake TestDbContext.

However if I resolve the fake context in my test setup, it isn't associated with any migrations (I assume because they are linked to the real context implementation). This has forced me to perform the migration synchronously in the call to ConfigureTestServices.

Performing the migration synchronously like this doesn't feel right, which leads me to two possible solutions:

How can I perform migrations using my test database context?

or

How can I have both the real and test database context classes registered in the IOC container so that it works in the tests and API simultaneously?

0

There are 0 best solutions below