0

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.

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?

Community
  • 1
  • 1
Phil Cooper
  • 3,083
  • 39
  • 63

1 Answers1

0

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

Community
  • 1
  • 1
stames
  • 1,250
  • 1
  • 10
  • 16
  • There doesn't appear to be any way to do this programmatically so this looks like the only way forward - thanks for answering. – Phil Cooper Nov 29 '13 at 08:23