9

我收到以下错误:

Test method: BootStrapperTest.Can_Create_Alert_Management_Object threw exception:  Ninject.ActivationException: 
Error activating IAlertManagement No matching bindings are available, and the type is not self-bindable. 

Activation path:   
1) Request for IAlertManagement

Suggestions:    
1) Ensure that you have defined a binding for IAlertManagement.    
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.    
3) Ensure you have not accidentally created more than one kernel.    
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.    
5) If you are using automatic module loading, ensure the search path and filters are correct.

这是导致此异常的测试用例:

[TestInitialize]
public void Initialize()
{
    BootStrapper.RegisterTypes();
}

[TestMethod]
public void Can_Create_Alert_Management_Object()
{
    IAlertManagement alertManagementService = BootStrapper.Kernel.Get<IAlertManagement>();

    Assert.IsNotNull(alertManagementService);
}

//This is the code that gets called in [TestInitialize]
public static void RegisterTypes()
{
    if (!initialized)
    {
        Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
                                   .SelectAllClasses()
                                   .BindDefaultInterface());

        Kernel.Unbind(typeof(IWcfServiceClient<>));
        Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx =>
                    (ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx)); 
    }

    initialized = true;
}

上述错误发生在我的构建服务器上的一个单元测试中,但没有发生在我的开发机器上。我有 7 个其他测试几乎与此测试相同,它们在构建服务器和我的开发机器上通过,但这是唯一失败的测试。

IAlertManagement 接口来自一个名为Core的 dll ,具体类型来自另一个名为AlertManagement的 dll 。我的单元测试项目中包含Core dll 和AlertManagement dll 作为项目引用。我有 7 或 8 个与这种情况相同的其他测试,但这是唯一失败的测试。

任何想法都会不胜感激。

4

3 回答 3

1

发生错误是因为IAlertManagement未与任何具体类绑定。尝试手动绑定IAlertManagement

public static void RegisterTypes()
{
    if (!initialized)
    {
        Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
                               .SelectAllClasses()
                               .BindDefaultInterface());


        //Try to add following line
        Kernel.Bind<IAlertManagement>().To<ConcreteImplementationOfIAlertManagement>();

        Kernel.Unbind(typeof(IWcfServiceClient<>));
        Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx => (ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx)); 
    }

    initialized = true;
}
于 2012-09-20T05:41:25.160 回答
1

我最终通过在我的单元测试项目中添加对解析类型的具体引用来解决这个问题。仅仅添加项目引用是不够的。这就是我这样做的方式:

[TestClass]
public class AssemblyInitialize
{
    /// <summary>
    /// Method which gets executed once per unit test session. The purpose of this method is to reference any loosely coupled
    /// assemblies so that they get included in the unit test session. If no code actually references a given assembly, even if its
    /// technically a project reference, it will not be copied to the test output folder.
    /// </summary>
    [AssemblyInitialize]
    public static void InitializeReferencedAssemblies(TestContext context)
    {
        List<Type> looselyCoupledTypes = new List<Type>
                                         {
                                             typeof(AlertManagement),
                                             typeof(Naming),
                                         };

        looselyCoupledTypes.ForEach(x => Console.WriteLine("Including loosely coupled assembly: {0}",
                                                           x.Assembly.FullName));
    }
}
于 2016-01-29T22:13:15.110 回答
0
  1. 我要检查的第一件事是确保包含实现的 DLLIAlertManagement被复制到构建服务器上的正确目录,以便测试看到它。

  2. 要尝试的另一件事是将内核加载代码移动到ClassInitialize函数而不是 a TestInitialize,只是为了查看是否存在某种竞争条件或其他相关事物。我在构建服务器上看到了随机错误,因为对象以不同的顺序或比通常发生的更快(在我的情况下涉及 Rx、TPL 和未观察到的异常)。可能是在构建服务器上并行运行的测试多于在桌面上并行运行的测试。

  3. 或者可能部分是由于服务器可能正在使用Server GC。我不知道强制它使用 Workstation GC 是否有帮助,但它可能值得一试。

于 2012-09-18T20:14:02.437 回答