我得到了以下将失败的测试用例:
预期:与 ArxScriptsTests.Engines.Ioc.Examples+A 相同但为:ArxScriptsTests.Engines.Ioc.Examples+A
问题是,如何做到正确?
[TestFixture]
public class Examples
{
public interface IInterface
{
}
public abstract class BaseClass : IInterface
{
}
public class A : BaseClass
{
}
public class B : BaseClass
{
}
[Test]
public void TestMethod1()
{
IKernel kernel = new StandardKernel();
// Bind to self
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<BaseClass>()
.BindToSelf()
.Configure(b => b.InSingletonScope())
);
// Bind to IInterface
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IInterface>()
.BindSelection((type, baseTypes) => new List<Type> { typeof(IInterface) })
.Configure(b => b.InSingletonScope())
);
// Bind to BaseClass
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<BaseClass>()
.BindSelection((type, baseTypes) => new List<Type> { typeof(BaseClass) })
.Configure(b => b.InSingletonScope())
);
List<IInterface> byInterface = new List<IInterface>(kernel.GetAll<IInterface>());
List<BaseClass> byBaseClass = new List<BaseClass>(kernel.GetAll<BaseClass>());
Assert.AreSame(byInterface[0], byBaseClass[0]);
}
}
一种解决方案是
[Test]
public void TestMethod1()
{
IKernel kernel = new StandardKernel();
// Bind to Both
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IInterface>()
.BindSelection((type, baseTypes) => new List<Type> { typeof(IInterface), typeof(BaseClass) })
.Configure(b => b.InSingletonScope())
);
List<IInterface> byInterface = new List<IInterface>(kernel.GetAll<IInterface>());
List<BaseClass> byBaseClass = new List<BaseClass>(kernel.GetAll<BaseClass>());
Assert.AreSame(byInterface[0], byBaseClass[0]);
}
但是当我尝试将两个绑定放在不同的模块中时,这将无济于事。或者这是一个坏主意吗?