简体   繁体   English

以编程方式进行单元测试SerializableConfigurationSection

[英]Unit test SerializableConfigurationSection programmatically

I'm using strongly typed configuration sections in my project and want to unit test a particular area which throws an exception when a setting isn't set-up correctly. 我在项目中使用了强类型的配置部分,并希望对特定区域进行单元测试,如果设置不正确,则会引发异常。

A snippet of the configuration class: 配置类的摘要:

public class EmailSettings : SerializableConfigurationSection, IEmailSettings
{
    [ConfigurationProperty("from", IsRequired = true)]
    public string From
    {
      get
      {
        ...
      }
      set
      {
        ...
      }
    }
    ...
}

Sample test method: 样品测试方法:

[TestMethod]
public void something_describing_this_test()
{
  EmailSettings settings = new EmailSettings();
  settings.From;
}

I expect that SerializableConfigurationSection and its inners are looking for a web.config (or similar) to read xml config from. 我希望SerializableConfigurationSection及其内部正在寻找web.config(或类似文件)以从中读取xml配置。

How can I get in the middle and 'mock' the configuration to enable me to pipe custom values to test for certain conditions? 我如何进入中间并“模拟”配置以使我能够通过管道传递自定义值来测试某些条件? This question (using ConfigurationManager methods) appears to do it via a physical config file in the assembly - is this the only way or can I get in there programmatically? 这个问题 (使用ConfigurationManager方法)似乎是通过程序集中的物理配置文件来完成的-这是唯一的方法还是可以以编程方式进入那里?

You could generate one in your test fixture setup, and then load it like described in the answer to this question: 您可以在测试治具设置中生成一个,然后按此问题的答案中所述加载它:

Loading custom configuration files 加载自定义配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM