如何测试 web.config 设置(尤其是按键)?

发布于 2024-09-14 04:01:58 字数 149 浏览 4 评论 0原文

测试 web.config 中的设置(尤其是按键)的好方法是什么?我认为它不能用 NUnit 进行测试,是吗?

示例:

感谢您的想法:-)

whats a good way to test the settings (especially keys) in web.config? I think its not really testable with NUnit, or is it?

Example: <add key="SomeKey" value="SomeValue" />

Thanks for ideas :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2024-09-21 04:01:59

“测试”是什么意思?至少我可以看到,在针对 .config 文件运行测试时,没有真正的价值,因为没有实际执行的代码,除非您编写了自定义 ConfigSectionHandler

如果您有应用程序所需的一组定义的键,您可能会认为值得将 System.Configuration.ConfigurationManager 的调用包装在配置帮助程序类中,该类公开您的应用程序特定的配置,将值解析为它们的值。适当的数据类型等等。这将是值得测试的东西,但不是 web.config 文件本身。例如:

using System;
using System.Configuration;

namespace MyApplication
{

    public sealed class MyApplicationConfiguration
    {

        public int NumberOfConnectionAttempts {get; private set;}
        public string ServerName {get; private set;}

        public MyApplicationConfiguration()
        {
            NumberOfConnectionAttempts = Convert.ToInt32(ConfigurationManager.AppSettings["ConnectionAtttempts"];
            ServerName = ConfigurationManager.AppSettings["ServerName"];
        }
    }
}

How do you mean "test"? There's no real value, that I can see at least, in running tests against a .config file, given that there's no code of yours actually executing, unless you've written a custom ConfigSectionHandler?

If you have a defined set of keys that your application requires, you might consider it worthwhile to wrap calls to System.Configuration.ConfigurationManager in a configuration helper class that exposes your application specific configuration, parses values into their appropriate datatypes and so on. That would be something that would be worth testing, but not the web.config file itself. For example:

using System;
using System.Configuration;

namespace MyApplication
{

    public sealed class MyApplicationConfiguration
    {

        public int NumberOfConnectionAttempts {get; private set;}
        public string ServerName {get; private set;}

        public MyApplicationConfiguration()
        {
            NumberOfConnectionAttempts = Convert.ToInt32(ConfigurationManager.AppSettings["ConnectionAtttempts"];
            ServerName = ConfigurationManager.AppSettings["ServerName"];
        }
    }
}
国粹 2024-09-21 04:01:59

您可以创建一个类,负责该类的配置参数和测试方法。

You can create a class that is responsible for configuration parameters and test methods of that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文