我有IWindsorContaner,它存在于整个应用程序生命周期中.对于Unittests,可以在其类型下注册模拟/存根等.当测试完成并且处理夹具时,用自创的称为"取消注册"的方法移除测试中的注册组件.
现在,我想更新到最新的Castle版本3.0.根据3.0发布说明的东西
public void Unregister(string contextName, string registrationName)
{
IWindsorContainer context = GetOrCreateContext(contextName);
context.Kernel.RemoveComponent(registrationName);
}
Run Code Online (Sandbox Code Playgroud)
因为已删除了IKernel.RemoveComponent方法,所以不再可能.解决这个问题的描述还不够("尝试使用IHandlerSelectors.").
我用于单元测试的夹具的简化版本:
public sealed class DependencyInjectionFixture : IDisposable
{
private Stack<Type> registeredTypes = new Stack<Type>();
// Registering of mocks/stubs, etc
public void RegisterSingleton<T>(T singleton, string objectName)
{
registeredTypes.Push(typeof(T));
IWindsorContainer context = GetOrCreateContext(contextName);
context.Register(Component.For(typeof(T))
.Named(objectName)
.Instance(singleton)
.LifeStyle.Singleton);
}
// Called when tests ends
public void Dispose()
{
IWindsorContainer context = GetOrCreateContext(contextName);
while (registeredTypes.Count > 0)
context.Kernel.RemoveComponent(CSApplicationContext.GetRegistrationNameFor(registeredTypes.Pop()));
}
Run Code Online (Sandbox Code Playgroud)
}
如何使用Castle 3.0删除组件?