【发布时间】:2026-02-15 10:50:01
【问题描述】:
我有一个包含多个项目的解决方案,其中一个项目是我的服务类,它调用持久性管理器。
我想写一个单元测试如下:
[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\Debug 文件夹中查找?
作为 Castle Windsor 的新手,我不知道应该怎么做才能进入 Castle Windsor 进行单元测试。
【问题讨论】:
标签: castle-windsor castle