我有一个Unit Test
项目&想要在Unit of Work
类中填充存储库的Stub实现。我会知道如何使用其他 IoC技术来完成它,但我是StructureMap的新手。
我无法在此找到好的文档。
DEFAULT CONTAINER:
以下是我希望覆盖的业务注册表的一部分。
public ContainerRegistry()
{
Scan(...);
// --------
// UNIT OF WORK
// This is the DEFAULT implementation
For(typeof(IRepository<>)).Use(typeof(GenericRepository<>));
For<IUnitOfWork>().Use<MeasurementContractsUnitOfWork>();
}
FOR UNIT TESTS:
在单元测试项目中,我希望IoC使用它:
// This is the STUB Implementation
For(typeof(IRepository<>)).Use(typeof(GenericRepositoryStub<>));
目标:
目标是像往常一样简单地使用容器,但让STUB以自动方式进行填充。
var container = IoC.Initialize();
var uow = container.GetInstance<IUnitOfWork>()
答案 0 :(得分:0)
我不确定这是否是正确的方法,但这就是我实现目标的方式。如果有人给出了更好的答案,我会将其标记为正确:
我创建了单独的IOC Initializer
&amp; ContainerRegistry
项目中的Unit Test
个类,如此......
INITIALIZER CLASS:
public static class IoC
{
public static IContainer Initialize()
{
var container = new Container();
// NOTE: If you have many Registries to consider, you can add them (order matters)
// The Business Registry
container.Configure(x => x.AddRegistry<Business.DependencyResolution.ContainerRegistry>());
// The UnitTest Projects Registry (order matters)
container.Configure(x => x.AddRegistry<ContainerRegistry>());
return container;
}
}
注册类:
public class ContainerRegistry : Registry
{
public ContainerRegistry()
{
// The Override
For(typeof(IRepository<>)).Use(typeof(GenericRepositoryIdentifiableStub<>));
}
}