多个项目的温莎城堡配置和单元测试

发布于 2024-12-12 02:05:10 字数 1509 浏览 0 评论 0原文

我有一个包含多个项目的解决方案,其中一个项目是我的服务类,它调用持久性管理器。

我想编写一个单元测试如下:

 [Test]
    public void Create_HappyPath_Success()
    {
        // Arrange
        UnitOfMeasure unitOfMeasure = new UnitOfMeasure();
        unitOfMeasure.Code = "Some new unit of measure";
        unitOfMeasure.DataOwner = 1;

        // Act
        this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

        // Assert something

    }

现在,我在这一行收到空引用异常:

 this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

我相信这是由于 Castle Windsor 没有被调用,因此 UoMService 没有被实例化。我的 Castle Windsor 应用程序安装程序是在另一个项目(即我的 ASP.NET MVC 项目)中定义的。所以我的第一个问题是是否可以重用该安装程序来运行我的单元测试。

现在为了解决这个问题,我通过链接到我的 Web 项目中的安装程序在我的单元测试项目中创建了一个新的安装程序。然后我在设置中使用了以下代码:

  [SetUp]
    public void ControllersInstallerTests()
    {
        this.containerWithControllers = new WindsorContainer();
        IoC.Initialize(this.containerWithControllers);

        this.containerWithControllers.Install(FromAssembly.This());
    }

这次当我运行测试时,出现以下错误:

SetUp:Castle.Windsor.Configuration.Interpreters.XmlProcessor.ConfigurationProcessingException:处理节点资源 FileResource 时出错:[] [] ----> Castle.Core.Resource.ResourceException:找不到文件 C:\Projects\DavidPM\Services\MyProject.Services.ServiceImpl.Test.Unit\bin\Debug\Config\Windsor.config

问题是为什么它在bin\调试文件夹?

作为温莎城堡的新手,我不确定应该做什么来连接温莎城堡进行单元测试。

I have a solution with multiple projects and one of these projects is my service class which calls into the persistence manager.

I would like to write a unit test as follows:

 [Test]
    public void Create_HappyPath_Success()
    {
        // Arrange
        UnitOfMeasure unitOfMeasure = new UnitOfMeasure();
        unitOfMeasure.Code = "Some new unit of measure";
        unitOfMeasure.DataOwner = 1;

        // Act
        this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

        // Assert something

    }

Now, I'm getting a null reference exception on this line:

 this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

I believe that it's due to the fact that Castle Windsor is not getting called and hence the UoMService isn't getting instantiated. My Castle Windsor application installer is defined in another project i.e. my ASP.NET MVC project. So my first question is whether it's possible to reuse that installer to run my Unit Tests.

Now to get around this problem, I created a new installer in my unit test project by linking to the installer in my web project. Then I used the following code in my set up:

  [SetUp]
    public void ControllersInstallerTests()
    {
        this.containerWithControllers = new WindsorContainer();
        IoC.Initialize(this.containerWithControllers);

        this.containerWithControllers.Install(FromAssembly.This());
    }

This time when I run the tests, I get the following error:

SetUp : Castle.Windsor.Configuration.Interpreters.XmlProcessor.ConfigurationProcessingException : Error processing node resource FileResource: [] []
----> Castle.Core.Resource.ResourceException : File C:\Projects\DavidPM\Services\MyProject.Services.ServiceImpl.Test.Unit\bin\Debug\Config\Windsor.config could not be found

The question is why is it looking in the bin\Debug folder?

As a newbie with Castle Windsor, I am not sure what I should be doing to hook into Castle Windsor for my unit tests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

万人眼中万个我 2024-12-19 02:05:10

您不应该在单元测试中连接 IoC 容器。在生产过程中,您的 IoC 容器将解决依赖关系。在单元测试期间,创建依赖项作为测试的一部分 - 通常使用模拟框架,以便您可以单独进行测试。

You should not be hooking up your IoC container in your unit tests. During production, your IoC container will resolve dependencies. During unit tests, you create the dependencies as part of your tests -- usually using a mocking framework so you can test in isolation.

青衫负雪 2024-12-19 02:05:10

将您的配置文件复制到输出目录

make your config file copy to output directory

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