如何在web.config中测试自定义配置节? [英] How to test custom configuration section in web.config?

查看:149
本文介绍了如何在web.config中测试自定义配置节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC网络API应用具有一个应用程序使用为配置值的自定义配置部分。

我想测试居住在web.config文件中的自定义配置节的值是否正确, GetSection 能够正确地阅读它们。有什么办法来测试这一点没有创造NUnit的另外一个配置文件完全相同的值?

解决方案
  

有什么办法来测试这一点没有创造NUnit的另外一个配置文件完全相同的值?

是的,你可以做到这一点,如下所示:

 配置OpenWebConfiguration(字符串configurationFilePath)
{
    VAR configurationFileInfo =新的FileInfo(configurationFilePath);
    VAR VDM =新VirtualDirectoryMapping(configurationFileInfo.DirectoryName,真实,configurationFileInfo.Name);
    VAR wcfm =新WebConfigurationFileMap();
    wcfm.VirtualDirectories.Add(/,VDM);
    返回WebConfigurationManager.OpenMappedWebConfiguration(wcfm,/);
}

...
变种C = OpenWebConfiguration(pathToWebConfigFile);
VAR节= c.GetSection(sectionName);
... 等等 ...
 

My MVC Web API application has a custom configuration section which is used by the application for config values.

I want to test whether the custom configuration section values populated in the web.config file are correct and GetSection is able to read them correctly. Is there any way to test this without creating another config file for nunit with the exact same values?

解决方案

Is there any way to test this without creating another config file for nunit with the exact same values?

Yes, you can do this as follows:

Configuration OpenWebConfiguration(string configurationFilePath)
{
    var configurationFileInfo = new FileInfo(configurationFilePath);
    var vdm = new VirtualDirectoryMapping(configurationFileInfo.DirectoryName, true, configurationFileInfo.Name);
    var wcfm = new WebConfigurationFileMap();
    wcfm.VirtualDirectories.Add("/", vdm);
    return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}

...
var c = OpenWebConfiguration(pathToWebConfigFile);
var section = c.GetSection(sectionName);
... etc ...

这篇关于如何在web.config中测试自定义配置节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆