views:

842

answers:

1

I'm trying to accomplish a seemingly super simple thing: from my unit test I want to replace the type being resolved with a mock/fake object.

For example: the xml config states that a component of the service IInterface should resolve to ClassA. That's fine, but from my unit test I want the type to resolve to FakeClassA instead. I can't use container.AddComponent for this, since there "is a component already registered for the given key ...".

+2  A: 

IKernel has a RemoveComponent method.

But for unit tests it's recommended that you don't use the container at all, or if the test setup gets too dense because of dependencies, use an AutoMockingContainer.

Here's another (more updated) implementation.

Mauricio Scheffer
RemoveComponent states "Returns true if the specified component was found and could be removed (i.e. no other component depends on it)". But what if we dont care if something else depends upon it, as we are going to replace it in the next line of code.
crowleym
You can't. Removing components from the container is *not* a normal usage in Windsor.
Mauricio Scheffer
Mauricio Scheffer: You have a dead link
Anthony
@Anthony: thanks, fixed it
Mauricio Scheffer