我将Ninject.Extensions.Factory与Ninject 3结合使用来创建工厂,该工厂根据提供给工厂的字符串创建不同类型的IFoo
。我已经通过了单元测试,但奇怪的是,仅在Resharper测试运行程序中。在NCrunch测试运行程序中,它失败。这是NCrunch配置问题,还是我需要更改代码?
界面:
public interface IFooFactory
{
IFoo CreateFoo(string name);
}
Ninject绑定(bind):
kernel.Bind<IFooFactory>().ToFactory(() => new UseFirstParameterAsNameInstanceProvider());
kernel.Bind<IFoo>().To<BarFoo>().Named("Bar");
考试:
[Test]
public void CanCreateFooTest()
{
var factory = (IFooFactory) Kernel.GetService(typeof(IFooFactory));
var bar = factory.CreateFoo("Bar");
Assert.AreEqual(typeof(BarFoo), bar.GetType());
}
和NCrunch异常:
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> Ninject.ActivationException : Error activating IInterceptor using conditional implicit self-binding of IInterceptor
Provider returned null.
Activation path:
2) Injection of dependency IInterceptor into parameter of constructor of type IFooFactoryProxy
1) Request for IFooFactory
Suggestions:
1) Ensure that the provider handles creation requests properly.
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Ninject.Infrastructure.Language.ExtensionsForIEnumerable.ToArraySlow(IEnumerable series, Type elementType) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerable.cs:line 29
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 149
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 95
at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Ninject.KernelBase.System.IServiceProvider.GetService(Type service) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 553
at FooProject.Tests.CanCreateFooTest() in C:\Projects\FooProject ...
--ActivationException
at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 165
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
最佳答案
这是与NCrunch一起使用的代码:
var kernel = new StandardKernel();
kernel.Bind<IFooFactory>().ToFactory(() => new UseFirstParameterAsNameInstanceProvider());
kernel.Bind<IFoo>().To<BarFoo>().Named("Bar");
kernel.Load<FuncModule>();
var factory = kernel.Get<IFooFactory>();
var bar = factory.CreateFoo("Bar");
Assert.Equal(typeof(BarFoo), bar.GetType());
更新
这很好用,并且可以解决NCrunch的问题。但是,Resharper提示它已被加载两次。解决方法:
#if NCRUNCH
Kernel.Load<FuncModule>();
#endif
关于visual-studio-2010 - Ninject ToFactory可在Resharper单元测试中使用,但不适用于NCrunch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10601098/