测试通过 Nuget 安装的 Ninject 项目 - WebActivate 行为
我正在尝试为使用 Ninject 的项目创建 NUnit 测试。 Ninject 是通过 Nuget 安装的,因此配置类看起来类似于这个简化版本:
[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectMVC3), "Stop")]
public static class NinjectMVC3
{
private static readonly Bootstrapper Bootstrapper = new Bootstrapper();
private static IKernel _kernel;
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
Bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
Bootstrapper.ShutDown();
}
}
我希望在我的启动测试类中调用这些方法。我试过:
[TestFixture]
public class TestBase
{
[SetUp]
public void Setup()
{
NinjectMVC3.Startup();
}
[TearDown]
public void TearDown()
{
NinjectMVC3.TearDown();
}
}
它不会工作,因为我正在尝试手动调用由 WebActivator 管理的方法。因此,我正在寻找一种方法来指示 WebActivator 在“正确的时间”调用这些方法。让我提醒您,我正在处理两个项目,一个是 MVC Web 项目(它使用 WebActivator for Ninject),另一个是我的 MVC Web 项目的测试项目。我尝试通过更改安装方法的实现来调用 WebActivator:
[SetUp]
public void Setup()
{
WebActivator.ActivationManager.Run();
}
它不起作用。据我了解,此调用 WebActivator 应该执行类似以下操作:
foreach (var assemblyFile in Directory.GetFiles(HttpRuntime.BinDirectory, "*.dll")) {
var assembly = Assembly.LoadFrom(assemblyFile);
foreach (PreApplicationStartMethodAttribute preStartAttrib in assembly.GetCustomAttributes(
typeof(PreApplicationStartMethodAttribute),
inherit: false)) {
preStartAttrib.InvokeMethod();
}
}
所以我猜它无法找到程序集。所以问题是 - 我怎样才能命令 WebActivator 扫描一些额外的程序集并在“正确的时间”触发一些方法。或者也许我在这里被误导了,为了测试我的 Ninject 项目,我应该采取不同的方法?
我能够在没有 WebActivator 的情况下测试我的解决方案,但由于它最近被广泛使用,我很想学习如何处理它并强制它做我想要的事情。
I am trying to create a NUnit test for a project that uses Ninject. The Ninject was installed via Nuget, so the Configuration clas looks similar to this simplified version:
[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectMVC3), "Stop")]
public static class NinjectMVC3
{
private static readonly Bootstrapper Bootstrapper = new Bootstrapper();
private static IKernel _kernel;
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
Bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
Bootstrapper.ShutDown();
}
}
I want those methods to be called in my startup test class. I tried:
[TestFixture]
public class TestBase
{
[SetUp]
public void Setup()
{
NinjectMVC3.Startup();
}
[TearDown]
public void TearDown()
{
NinjectMVC3.TearDown();
}
}
It will not work because I am trying to manually call methods that are managed by WebActivator. So I am looking for a way to instruct WebActivator to call those methods in a 'right time'. Let me remind you that there are two project that I am dealing with, one is a MVC Web Project (and it uses WebActivator for Ninject), and the other one is a Test project for my MVC Web Project. I tried to call WebActivator by changing implementation of my Setup method:
[SetUp]
public void Setup()
{
WebActivator.ActivationManager.Run();
}
It doesn't work. As far As I understand underneath this call WebActivator should do something similar to:
foreach (var assemblyFile in Directory.GetFiles(HttpRuntime.BinDirectory, "*.dll")) {
var assembly = Assembly.LoadFrom(assemblyFile);
foreach (PreApplicationStartMethodAttribute preStartAttrib in assembly.GetCustomAttributes(
typeof(PreApplicationStartMethodAttribute),
inherit: false)) {
preStartAttrib.InvokeMethod();
}
}
So I guess that it is unable to find an assembly. So the question is - how can I order WebActivator to scan thru some additional assembly and fire some methods in a 'right time'. Or maybe I am mislead here, and in order to test my Ninject project I should take a different approach?
I am able to test my solutions w/o WebActivator, but because it is widely used recently, I am keen to learn how to deal with it and force it to do things that I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会避免在您的测试项目中使用 WebActivator,因为它在 ASP.NET 之外无法正常运行。
如果您想测试 Ninject 内核的设置,我会将 CreateKernel() 方法公开并从您的 Setup() 方法中调用该方法。
...
I would avoid using WebActivator from your test project as it will not play well outside of asp.net.
If you want to test the setup of your Ninject kernel than i would make the CreateKernel() method public and call that from your Setup() method.
...
不幸的是,默认情况下,WebActivator 在 ac:\tmp... 目录中查找“*.dll”,因此它无法找到解决方案中包含的项目库。
我最终获得了源代码并将以下代码添加到 ActivationManager 类中:
在测试类中:
这是丑陋的代码,我希望有一天能看到更好的方法。
Unfortunately by default WebActivator looks for a "*.dll" in a c:\tmp... directory, and due to that it is unable to find project libriaries that are included to the solution.
I ended up geting the source code and adding a following code to the ActivationManager class:
And in test class:
This is ugly code, and I hope to see one day a better approach.