我有一个继承自IWindsorInstaller的类。我使用这个类来安装/注册依赖项。现在我想为这个类编写测试。
我在容器中注册了一个带参数的依赖项。像这样:
container.Register(Component.For<IXService>().ImplementedBy<XService>().DependsOn(Dependency.OnComponent("operationY", "OperationY")).LifeStyle.Singleton);
现在在我的单元测试中,我想验证它是否已正确注册。像这样:
_containerMock.Verify(f=>f.Register(It.IsAny<ComponentRegistration<IXService>>().ImplementedBy<XService>().DependsOn(Dependency.OnComponent("operationY", "OperationY")).LifestyleSingleton()),Times.AtLeastOnce);
我不知道,我该怎么办? 在此先感谢,莫。
答案 0 :(得分:0)
您的单元测试不能证明容器具有给定服务的注册。
证明容器可以解析该服务(存在注册的事实是实现细节)。
因此,在您的单元测试中,只需尝试解析您的服务并验证它是您要找的内容,例如:
public void UnitTest_Prove_That_Service_Can_Be_Created()
{
var sut = new SystemUnderTest();
var container = sut.GetContainer();
var service = container.Resolve<IXService>();
// Prove the IXService resolves to an XService - i.e. the registration
// has the correct mapping.
Assert.Type<XService>(service);
}
此外,您可能希望证明它是单身人士:
public void UnitTest_Prove_That_Service_Is_A_Singleton()
{
var sut = new SystemUnderTest();
var container = sut.GetContainer();
var service1 = container.Resolve<IXService>();
var service2 = container.Resolve<IXService>();
// Prove you got the same service back each time - i.e. it's a singleton
Assert.Equals(service1, service2);
}
SUT是IWindsorInstaller
的事实并没有真正改变我的答案。只需稍微调整单元测试:
public void UnitTest_Prove_That_Service_Can_Be_Created()
{
var container = new WindsorContainer();
container.Install(new SystemUnderTest());
var service = container.Resolve<IXService>();
// Prove the IXService resolves to an XService - i.e. the registration
// has the correct mapping.
Assert.Type<XService>(service);
}