【发布时间】:2014-05-03 13:31:57
【问题描述】:
我对这两个库都是新手,在将它们用于大型项目之前,我需要澄清我的单元测试中低代码工作量自动模拟的选项。
在 Google 上花了一些时间后,我得出的结论是,与其他一些 IOC/Mocking 产品配对不同,LightInject+Nsubstitute 没有现成的插件库来简化在排列阶段的无操作默认模拟的声明一个单元测试。
我已阅读 LightInject 文档,了解如何使用临时增强的模拟对象覆盖 LightInject 容器,仅用于单元测试的范围但是单元测试可能涉及的所有无操作默认隔离模拟呢? .有没有办法在 LightInject 容器中自动创建它们?
我正在寻找的内部 IOC 容器行为是:
public class LightInject.ServiceContainer
{
..
public T GetInstance<T)
{
if (( this.RegisteredInterfaces.Any( i => i.Itype == T ) == false )
&& ( this.TemporaryUnitTestOverrides.Any( i => i.Itype == T ) == false ))
&& ( /* this container is configured with an automocking delegate */ ))
return autoMockCreatorDelegate<T>.Invoke();
}
LightInject 的 IProxy 和 Interceptors 似乎提供了一些内部模拟对象构建块,但相比之下,Nsubstitute 库功能齐全。
澄清我的意思是默认不做任何模拟和增强模拟。
// default do nothing mock
var calculator = Substitute.For<ICalculator>();
// Enhanced mock that will return 3 for .Add(1,2)
var calculator = Substitute.For<ICalculator>();
calculator.Add(1, 2).Returns(3);
显然,第二种增强的模拟类型需要在每个单元测试中本地制作。
【问题讨论】:
标签: ioc-container nsubstitute automocking light-inject