I’m trying to unit test a custom ConfigurationSection I’ve written, and I’d like to load some arbitrary configuration XML into a System.Configuration.Configuration for each test (rather than put the test configuration xml in the Tests.dll.config file. That is, I’d like to do something like this:
Configuration testConfig = new Configuration('<?xml version=\'1.0\'?><configuration>...</configuration>'); MyCustomConfigSection section = testConfig.GetSection('mycustomconfigsection'); Assert.That(section != null);
However, it looks like ConfigurationManager will only give you Configuration instances that are associated with an EXE file or a machine config. Is there a way to load arbitrary XML into a Configuration instance?
There is actually a way I’ve discovered….
You need to define a new class inheriting from your original configuration section as follows:
You can then instantiate your ConfigurationSection object as follows:
Hope it helps someone 🙂