从NUnit运行时,Ninject无法绑定

时间:2015-02-27 09:27:32

标签: c# nunit ninject

我遇到了一个问题,我不知道如何最好地解决,理想情况下不需要重做代码。

序幕: 我交给现有的应用程序以维护并在必要时进行升级。它是一个C#应用程序。现在,当谈到c#时,我不是专业人士,但是我很擅长完成任务,而不会在阅读我的代码时让任何人眼睛流血。 问题是,应用程序使用NUnit(不是那么大的问题,我有经验,我理解基础)和Ninject(这是一个不同的故事)。

问题: 该应用程序使用Ninject将几个类绑定到内核。我的目标是稍微修改应用程序以支持某些不同的对象。

现在,当我调试应用程序或构建它并进行部署时,它可以工作。 但是,运行Nunit,我得到一个 Ninject.ActivationException:激活IDatabaseConnection 异常时出错。 (IdatabaseCnnection是内部库的接口。

我尝试重新创建在控制台应用程序中失败的测试,它也在那里工作,它只在通过NUnit运行时失败

Ninject.ActivationException: Error activating IDatabaseConnection
No matching bindings are available, and the type is not self-bindable.
Activation path:
  3) Injection of dependency IDatabaseConnection into parameter databaseConnection of constructor of type OrderMailDefinitionSource
  2) Injection of dependency IMailDefinitionSource into parameter mailDefinitionSource of constructor of type DocumentMailService
  1) Request for DocumentMailService

Suggestions:
  1) Ensure that you have defined a binding for IDatabaseConnection.
  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.

据我所知,出于某种原因,在运行NUnit时,绑定失败了。以任何方式运行它,而不是通过NUnit,它可以工作。

如果您有任何想法,我会感激不尽。 祝你有愉快的一天。

答案 这是问题(最初没有使用CodeBase属性)。 Nunit没有复制DLL,因此无法加载大约21个不同的dll。在修好之后,它起作用了。

 kernel.Bind(x => x.FromAssembliesInPath(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath))
                           .SelectAllClasses()
                           .BindDefaultInterface());

0 个答案:

没有答案