Ninject ToFactory在Resharper单元测试中工作,但不适用于NCrunch

时间:2012-05-15 12:52:50

标签: visual-studio-2010 unit-testing ninject ncrunch ninject-3

我正在使用Ninject.Extensions.Factory和Ninject 3创建一个工厂,它根据提供给工厂的字符串创建不同类型的IFoo。我有一个通过单元测试,但奇怪的是,只有在Resharper测试运行器。在NCrunch测试运行器中,它失败了。这是NCrunch配置问题,还是需要更改代码?

界面:

public interface IFooFactory
{
    IFoo CreateFoo(string name);
}

Ninject绑定:

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)

4 个答案:

答案 0 :(得分:10)

进入单元测试库的NCrunch配置,并将将引用的程序集复制到工作区设置为 True

NCrunch confuguration screenshot

答案 1 :(得分:7)

这里的代码适用于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

答案 2 :(得分:5)

使用该TestRunner运行时未加载FuncModule。如果未将扩展名复制到已执行进程的启动目录,则会发生这种情况。

我没有NCrunch。所以我不能告诉你它在做什么。但很可能它以与R#测试运行器不同的方式复制程序集。您可以手动加载扩展程序,但这感觉就像一个黑客。

答案 3 :(得分:2)

我一直在使用harriyott的建议一年左右。但是这个问题也发生在我们的TFS-Buildserver上。所以现在我避免Ninject自动加载所有扩展并手动加载它们。这避免了#if#endif,并且相同的代码将在Resharper和NCrunch上运行:

var kernel = new StandardKernel(new NinjectSettings { LoadExtensions = false});
kernel.Load<FuncModule>();

其余的不变:

kernel.Bind<IFooFactory>().ToFactory(() => new UseFirstParameterAsNameInstanceProvider());
kernel.Bind<IFoo>().To<BarFoo>().Named("Bar");

var factory = kernel.Get<IFooFactory>();
var bar = factory.CreateFoo("Bar");
Assert.Equal(typeof(BarFoo), bar.GetType());