我有一个组件可以从标准 .NET 配置 (app.config) 文件中读取一些配置。
当我(使用 TD.NET)为此组件运行单元测试(NUnit)时,我注意到配置文件未被读取。
检查 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 后,
我注意到它的值设置为 C:\Users\ltal\AppData\Local\Temp\tmp6D2F.tmp (一些临时随机位置)。
发生这种情况有原因吗? (是NUnit还是TD.NET的错?)
我想我可以为了测试而自己设置这个SetupInformation对象,还没有尝试过,但仍然想知道为什么它是这样创建的,而不是默认的。
I have a component that reads some configuration from the standard .NET configuration (app.config) file.
when I run unit tests (NUnit) for this component (using TD.NET), i noticed that the configuration file is not read.
Upon inspection of AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
I have noticed that its' value is set to C:\Users\ltal\AppData\Local\Temp\tmp6D2F.tmp (some temp random locaiton).
Is there a reason for why this is happening? (Is it NUnit or TD.NET's fault?)
I suppose i could set this SetupInformation object myself for the sake of the test, haven't tried yet, but still wondering why is it being created like that and not as default.
发布评论
评论(2)
要解决此问题,您可以在单元测试项目中创建一个 app.config。然后,单元测试将调用它来代替主 app.config。然后,您可以在单元测试中更改该 app.config 中的值,从而更轻松地测试不同的值和配置,即您可以在运行测试之前使用某些值设置测试 app.config。
ConfigurationManager.AppSettings[""] = "";
另一个选项可能是将设置放在主项目的 Settings.setting 文件中。那么您不必更改单元测试项目中的任何内容。有关设置和 app.config 之间差异的一些链接 - MSDN 论坛,StackOverflow,用户设置-MSDN
当然,第三种选择是通过引入接口并将依赖项注入到组件中,从而轻松模拟它,从而从组件中删除对 app.config 的依赖项和单元测试。
To workaround this, you can create an app.config in your unit test project. This will then be called in place of the main app.config by your unit tests. You can then change values in that app.config in your unit tests making it easier to test different values and configurations i.e. you can setup your test app.config with certain values before running your test.
ConfigurationManager.AppSettings[""] = "";
Another option might be to place settings in the Settings.setting file of your main project. You do not have to change anything in your unit test project then. Some links about the difference between settings and app.config - MSDN forums, StackOverflow, User Settings - MSDN
And of course a third option would be to remove the dependency on the app.config from your component by introducing an interface and inject the dependency into the component making it easy to mock it out and unit test.
默认情况下,.NET 运行时会查找 AppDomain 的工作目录,该目录由临时位置中的 NUnit 进行管理。
此链接提供了两种有关如何获取配置文件的解决方案:
http://blogs.msdn.com/b/josealmeida/archive/2004/05/31/loading-config-files-in-nunit.aspx
基本上,他们需要居住在测试目录。
By default the .NET runtime looks in the working directory of the
AppDomain
, which is being managed by NUnit in the temp location.This link offers two solutions about how to get config files picked up:
http://blogs.msdn.com/b/josealmeida/archive/2004/05/31/loading-config-files-in-nunit.aspx
Basically, they need to live in the testing directory.