Registering a mock/stub with Windsor

I have some code that uses Castle Windsor to retrieve instances this helps to manage dependencies that I don’t have to worry about, the side effect of this is that when performing unit tests against the code that needs to get an instance from Windsor I want to provide a way to inject a mock/stub object is can be accomplished with the following:

container.Register(Component.For()
                                        .Instance(mockFoo));

To help reduce the amount of code in my test setups I usually have this method in my Unit Testing base class

protected void RegisterDependencyAgainst(TInterface instance)
{
    container.Register(Component.For()
                                .Instance(instance));
}

Which is then called like this:

// in setup method
mockFoo = MockRepository.CreateMock();
RegisterDependencyAgainst(mockFoo);
Advertisements

3 thoughts on “Registering a mock/stub with Windsor

  1. Right here is the right blog for anyone who would like to understand this topic. You know so much its almost tough to argue with you (not that I really will need to…HaHa). You definitely put a new spin on a subject that has been discussed for a long time. Excellent stuff, just excellent!

Comments are closed.