测试通过 Nuget 安装的 Ninject 项目 - WebActivate 行为

发布于 2024-12-25 07:58:10 字数 1970 浏览 2 评论 0原文

我正在尝试为使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄兵必败 2025-01-01 07:58:10

我会避免在您的测试项目中使用 WebActivator,因为它在 ASP.NET 之外无法正常运行。

如果您想测试 Ninject 内核的设置,我会将 CreateKernel() 方法公开并从您的 Setup() 方法中调用该方法。

public static IKernel CreateKernel()

...

[SetUp]
public void Setup()
{
    NinjectMVC3.CreateKernel();
}

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.

public static IKernel CreateKernel()

...

[SetUp]
public void Setup()
{
    NinjectMVC3.CreateKernel();
}
灼疼热情 2025-01-01 07:58:10

不幸的是,默认情况下,WebActivator 在 ac:\tmp... 目录中查找“*.dll”,因此它无法找到解决方案中包含的项目库。

我最终获得了源代码并将以下代码添加到 ActivationManager 类中:

public static void AddAssembly(Assembly assembly)
{
    if (_assemblies == null)
    {
        _assemblies = new List<Assembly>();
    }
    _assemblies.Add(assembly);
}

在测试类中:

 private const int PreStartInitStage_DuringPreStartInit = 1;
    [SetUp]
    public void Setup(){
        WebActivator.ActivationManager.AddAssembly(Assembly.GetAssembly(typeof(NinjectMVC3)));
        typeof(BuildManager).GetProperty("PreStartInitStage", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, PreStartInitStage_DuringPreStartInit, null);
        WebActivator.ActivationManager.RunPreStartMethods();
        Kernel = NinjectMVC3.GetKernel();
    }

这是丑陋的代码,我希望有一天能看到更好的方法。

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:

public static void AddAssembly(Assembly assembly)
{
    if (_assemblies == null)
    {
        _assemblies = new List<Assembly>();
    }
    _assemblies.Add(assembly);
}

And in test class:

 private const int PreStartInitStage_DuringPreStartInit = 1;
    [SetUp]
    public void Setup(){
        WebActivator.ActivationManager.AddAssembly(Assembly.GetAssembly(typeof(NinjectMVC3)));
        typeof(BuildManager).GetProperty("PreStartInitStage", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, PreStartInitStage_DuringPreStartInit, null);
        WebActivator.ActivationManager.RunPreStartMethods();
        Kernel = NinjectMVC3.GetKernel();
    }

This is ugly code, and I hope to see one day a better approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文