多个项目的温莎城堡配置和单元测试
我有一个包含多个项目的解决方案,其中一个项目是我的服务类,它调用持久性管理器。
我想编写一个单元测试如下:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在单元测试中连接 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.
将您的配置文件复制到输出目录
make your config file copy to output directory