如何解决使用 MOQ 对 Unity 容器注册进行单元测试
我在生产代码中有以下内容: 对于此类注册:
public void setup()
{
this.VegetableSetup.Register<VegetableModule>();
}
我尝试过的单元测试通过了:
[TestMethod]
public void VegetableSetup_Tester()
{
// Arrange
var aVegLoader = new VegLoader(this.VegetableSetupMock.Object);
// Act
aVegLoader.setup();
// Assert,VegetableSetupMock is mock of VegetableSetup
this.VegetableSetupMock.Verify(VegetableSetupMock=> VegetableSetupMock.Register<VegetableModule>(),Times.Once);
}
以上单元测试通过。但是下面的测试失败了。
public FruitLoader(IUnityContainer theContainer)
{
this.myContainer = theContainer;
}
public void setup()
{
this.myContainer.RegisterType<IFruits,FruitsModel>(
new HierarchicalLifetimeManager(),new InjectionConstructor(
new ResolvedParameter<ICommunicationClient>(FruitConstants.CommunicationChannel),typeof(IFruitInfo),typeof(IContainerData)));
}
上面的注册测试有点复杂。
我曾经如何尝试为 Fruit 类设置做同样的事情。但不起作用。
[TestMethod]
public void FruitTester()
{
// Arrange
var aFruitLoader = new FruitLoader (this.ContainerMock.Object);
// Act
aFruitLoader .setup();
// Assert
this.myContainerMock.Verify(ContainerMock => ContainerMock .RegisterType<IFruits>(),Times.Once);
}
这是失败的。我该如何进行这个单元测试?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。