I have an ASP.NET MVC app that has a NInject container for dependency injection. As advertised, IoC makes it pretty easy for me to test components in isolation, and then easy to compose components in the application.
I want to test my use of the DI. I don't want to test that the DI component operates correctly. I trust that the NInject folks do that pretty well. I want to test that I have used the DI to compose components correctly with respect to my application's intentions. I don't want to test NInject, I want to test my use of NInject. (I also don't want to try to categorize this as a unit test or an integration test. I only want to be able to, sometime before I get to production, demonstrate why I have confidence that my particular use of NInject works.)
Suppose I have something like:
private static void RegisterDatabaseConnections(IBindingRoot kernel)
{
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
kernel.Bind<IProvideDbConnections>()
.To<Connector>()
.WhenInjectedInto<ActivityStore>()
.WithConstructorArgument("connectionString", connectionString);
kernel.Bind<IProvideDbConnections>()
.To<Connector>()
.WhenInjectedInto<CrConnector>()
.WithConstructorArgument("connectionString", connectionString);
kernel.Bind<IProvideDbConnections>()
.To<CrConnector>()
...
}
Somewhere I have:
public class Writer
{
public Writer(IProvideDBConnections connector)
{
Connector = connector;
}
private IProvideDBConnections Connector { get; set; }
....
}
I would like to write an MSTest like
[TestMethod]
public void TestThatWriterCrConnectorContainsConnector()
{
...
}
so that I can verify that I wrote RegisterDatabaseConnections
to give me what I expect. I am having trouble getting started down that path, because I don't know to access a kernel to set up any tests. The code that starts the kernel in the application is a collection of static private methods, and is a little mysterious to me, and I don't see how I would do this in the context of a test. Any help?
I would create my own kernel and
Load()
the module(s) you are wanting to test.this is assuming you have defined all your bindings that you want to test in a
NinjectModule