I am experiencing an issue with v1 of Ninject and resetting the StandardKernel. I have a static object that I use to provide access to the kernel like so
public static class ObjectFactory
{
private static IKernel _kernel = new StandardKernel(new CanceisModule());
// Resolve methods snipped for brevity
public static void Reset()
{
_kernel = null;
_kernel = new StandardKernel(new CanceisModule());
}
}
The problem comes when I try to use ObjectFactory in various unit tests (I use MSTest) or fitnesse fixtures. I always call the ObjectFactory.Reset() method before every new test or fixture but sometimes it seems like the Reset doesn’t actually work and leaves the original bindings in place. I know there is a way to reset the IKernel objects in v2 of Ninject but we aren’t ready to make that move yet (and its a fairly significant move for us).
Could someone offer some advice on why this might be occuring? I’m guessing that it is related to the way tests are executed on separate threads in the different runners but how do I avoid it?
Thanks in advance
Your approach seems fine.
In general, test runners don’t run multi threaded or anything magic like you’re guessing.
I’d suggest putting a breakpoint in your Reset() and debugging your tests.
Your code has a non-static method in a static class so is there something else you’re omitting to mention?
When you say “new test or fixture”, which is it? Often it you are using Ninject in tests, it’s better to keep a kernel in a base class and make sure it’s reset at the right time to avoid any confusion or doubt [rather than relying on an object factory ‘singleton’].