harriyott.com

Ninject Controller Testing

I've been converting an old, large project to use NInject to inject services and repositories into my controllers, removing the base controller's code that newed them up. I wrote a handy unit test to make sure all the bindings worked for the controllers' constructors, which caught a few missing bindings before it passed.

[TestMethod]
public void Can_create_controllers()
{
    var kernel = new StandardKernel();
    SiteResolver.RegisterComponents(kernel);

    typeof(AccountController)
        .Assembly
        .GetTypes()
        .Where(t => typeof(Controller).IsAssignableFrom(t))
        .ToList()
        .ForEach(c => kernel.Get(c));

    Assert.IsTrue(true);
}
24 October 2013