Mat*_*tin 163 .net c# nunit configurationmanager app-config
我正在尝试完成此异常处理程序:
if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
string pathOfActiveConfigFile = ...?
throw new ConfigurationErrorsException(
"You either forgot to set the connection string, or " +
"you're using a unit test framework that looks for "+
"the config file in strange places, update this file : "
+ pathOfActiveConfigFile);
}
Run Code Online (Sandbox Code Playgroud)
当我使用nUnit时,这个问题似乎只发生在我身上.
Céd*_*Rup 352
试试这个
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
Ric*_*ard 62
严格来说,没有单一的配置文件.除了ASP.NET 1,可以使用inbuilt(System.Configuration)支持三个配置文件.除了机器配置:app.exe.config,用户漫游和用户本地.
要获得"全局"配置(exe .config):
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.FilePath
Run Code Online (Sandbox Code Playgroud)
ConfigurationUserLevel为每次使用的漫游和非漫游配置文件使用不同的值.
1具有完全不同的模型,其中子文件夹(IIS-虚拟或文件系统)的内容web.config可以(取决于设置)添加或覆盖父文件夹web.config.
如果您的意思是在使用NUnit时只获得空返回,那么您可能需要将应用程序的app.config的ConnectionString值复制到测试库的app.config.
当它由测试加载器运行时,测试程序集在运行时加载,并将查找自己的app.config(在编译时重命名为testAssembly.dll.config),而不是您的应用程序配置文件.
要获得正在运行的程序集的位置,请尝试
System.Reflection.Assembly.GetExecutingAssembly().Location
Run Code Online (Sandbox Code Playgroud)
我第一次意识到单元测试项目引用了该项目中的app.config而不是与我的生产代码项目相关的app.config(当然,DOH)我刚刚在Prod项目的Post Build事件中添加了一行这会将app.config复制到测试项目的bin文件夹中.
问题解决了
到目前为止,我还没有注意到任何奇怪的副作用,但我不确定这是否是正确的解决方案,但至少它似乎有效.
我看到这里缺少另一个选项:
const string APP_CONFIG_FILE = "APP_CONFIG_FILE";
string defaultSysConfigFilePath = (string)AppDomain.CurrentDomain.GetData(APP_CONFIG_FILE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135216 次 |
| 最近记录: |