dimanche 8 mars 2015

Unit testing async database methods

I have a Web API that I want to test using MS Test. I want to test one controller in particular. This is the init code:



public class MyControllerTests
{
private static MyController controller;

[AssemblyInitialize]
public static void Initialize(TestContext context)
{
controller = new MyController();

IoC.Register(Component.For<IDbContextFactory>().ImplementedBy<MockDbContextFactory>());
// other config
}
// test methods, all async
}


This is the mock context factory. Is it used inside all the API methods to get a DB context.



public class MockDbContextFactory : IDbContextFactory
{
MyContext context;

public MyBaseContext GetContext()
{
if (context == null)
{
context = new MyContext(new DropCreateDatabaseAlways<MyContext>());

//populate with mock data...
}

return context;
}
}


Everything was nice until I added a test for the delete method. It is finished before the other methods, so it deletes objects from the shared context and the other tests fail. No I have two ideas: new context per method (using a [TestInitialize] resetter), but the underlying database is still the same and I get lots of key conflicts when inserting the new mock objects. Another idea is setting a new database in memory and having completely separate instances. I found Effort, but I believe this is an overkill and I'm approaching it wrong.


I am using Castle Windsor as the IoC container, in case there is a way to do it on the IoC level.


Aucun commentaire:

Enregistrer un commentaire