2016-02-10 33 views
0

我有靜態類的靜態方法,注入我的依賴關係:如何讓Ninject內核返回NSubstitute模擬對象?

public static void InjectDependency(NinjectModule module); 

我有靜態屬性來獲取每個對象的實例:

public static IKernel AppKernel {get;set;} 

如果我想創建一個實例,根據我injeced NinjectModule派生類中,我使用下面的代碼:

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

但現在我想我的內核返回模擬Ø bject,用NSubstitute創建。怎麼可以做到這一點?

回答

1

看到這個Ninject MockingKernel,這個時候您正在測試您不使用new StandardKernel(),您將使用new NSubstituteMockingKernel() - 你的情況,但MockingKernelRhinoMocks支持FakeItEasy **和**起訂量

+0

你可以提供一個小例子,好嗎? – klutch1991

0

您可以只使用StandardKernel並綁定替代:

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

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