如何制作 Ninject 内核 return NSubstitute 模拟对象?

How can I make Ninject kernel return NSubstitute mock object?

我在 static class 中有静态方法,它注入了我的依赖项:

 public static void InjectDependency(NinjectModule module);

我有静态 属性 来获取每个对象的实例:

 public static IKernel AppKernel {get;set;}

如果我想创建一个实例,取决于我注入的 NinjectModule-派生的 classes,我使用以下代码:

 IInterface instance = MyStaticClass.AppKernel.Get<IInterface>();

但现在我想制作我的内核 return 模拟对象,使用 NSubstitute 创建。怎么做到的?

看到这个Ninject MockingKernel,当你测试的时候你不使用这个new StandardKernel(),你将使用new NSubstituteMockingKernel()-在你的情况下,但是 MockingKernel 支持 RhinoMocksFakeItEasy ** 和 **起订量.

您可以只使用 StandardKernel 并绑定替代品:

var kernel = new StandardKernel();
// Set up kernel

var substitute = Substitute.For<ISubstitutedObject>();
kernel.Bind<ISubstitutedObject>().ToContstant(substitute);