Why I like Ninject for Acceptance Testing (.NET Dependency Injection Container)

Lately, I wrote quite a lot of acceptance tests. I simulate that the application is started-up and perform all features requested by our customer to make sure that they work as expected. In order to make these tests fast so that they can be run on every commit to the version control system, I simulate all interaction with the environment of the application: views, database, file system, registry, share point and so on.

To get the most out of these acceptance tests, I want to fake as little as possible. And here comes the Rebind feature of Ninject into play. I can bootstrap the application as in production and then replace all components interacting with the environment by simple calls to Rebind.

Acceptance test in pseudo-code:

IKernel kernel = Create Ninject Kernel and load all modules like in production;

var acceptanceTestModule = new AcceptanceTestModule();
Create all stubs and pass them to acceptanceTestModule;


Call bootstrapper to start-up application like in production;

Perform the action you want to check;

Do some checks;

And the AcceptanceTestModule looks like:

public class AcceptanceTestModule : NinjectModule
    public ISomeView SomeViewStub { get; set; }
    public IRegistry RegistryStub { get; set; }
    public IFileSystem FileSystemStub { get; set; }

    public override Load()

And everything else is run like in production.

One last thing: keep in mind that Rebind overrides all previous Bindings on the target type.

Why I like … posts are not about a tool or process being better that others, it’s just my thoughts what makes them useful for me in my projects.

About the author

Urs Enzler

1 comment

By Urs Enzler

Recent Posts