所以我有以下的类,它需要在其构造函数中,相同的接口依赖的三种不同的实现方式:自定义复杂的依赖关系的类型的创建与AutoFixture
public class MyTestClass : ISomeInterface<string>
{
// Constructor
public MyTestClass([Named("ImplementationA")] IMyTestInterface implementationA,
[Named("ImplementationB")] IMyTestInterface implementationB,
[Named("ImplementationC")] IMyTestInterface implementationC)
{
// Some logic here
}
public void MethodA(string)
{
}
}
当使用这个类外单元测试,我注入的依赖与Ninject问题,即我有这样的事情:
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
this.Bind<IMyTestInterface>().To<ImplementationA>().InRequestScope().Named("ImplementationA");
this.Bind<IMyTestInterface>().To<ImplementationB>().InRequestScope().Named("ImplementationB");
this.Bind<IMyTestInterface>().To<ImplementationC>().InRequestScope().Named("ImplementationC");
}
}
,工作正常,但我现在遇到的问题是,我想单元测试这个类,我想用AutoFixture来做到这一点,这引起了我的兴趣我的问题,我怎么创建MyTestClass与IMyTestInterface,这三个具体实现的实例,即ImplementationA,ImplementationB,并ImplementationC?如果我只是做这样的事情:
private ISomeInterface<string> testInstance;
private Fixture fixture;
[TestInitialize]
public void SetUp()
{
this.fixture = new Fixture();
this.fixture.Customize(new AutoMoqCustomization());
this.testInstance = this.fixture.Create<MyTestClass>();
}
AutoFixture创建MyTestClass的实例,但IMyTestInterface,这不是我想要的依赖关系的一些随机的实现。我一直在寻找一个在线答案,迄今为止我发现的唯一东西似乎与我所需要的类似,但不完全是,this
它看起来像你可以从'ISpecimenBuilder'继承,并使用AutoFixture创建每个特定的接口实现:[见这个答案](https://stackoverflow.com/questions/26149618/autofixture-customizations-provide-constructor-参数)和[这个答案](https://stackoverflow.com/questions/16819470/autofixture-automoq-supply-a-known-value-for-one-constructor-parameter/16954699#16954699) –