The Freak Parade

Strange noises from the mind of Nathan Stults…
  • rss
  • Home
  • About The Freak Parade

Unit Testing Custom .NET Configuration Classes

May 15, 2008

I am currently in the process of writing some custom .NET Configuration classes, such as custom ConfigurationHandler implementations, ConfigurationElementCollection, etc. Configuration classes need to be tested just like any other code, especially if they contain any behavior beyond the simple mapping of an XML property to a class property. It turns out the .NET Configuration API makes this pretty easy:

 

    [TestFixture]
    public class ConfigurationTestFixture
    {
        [Test]
        public void TestCustomConfig()
        {
            var cfg = GetTestConfiguration();
            Assert.That(cfg, Is.Not.Null);

            var mySection = cfg.GetSection("mySection") as mySection;
            Assert.That(mySection, Is.Not.Null);
            Assert.That(mySection.CustomProp,Is.EqualTo("configuredValue"));
            ...

        }

        private System.Configuration.Configuration GetTestConfiguration()
        {
            var map = new ExeConfigurationFileMap()
                          {
                              ExeConfigFilename =
                              Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                              "TestApp.config")
                          };

            return ConfigurationManager.OpenMappedExeConfiguration(
                    map, ConfigurationUserLevel.None);
        }

 

For this to work you’ll need a configuration file with the appropriate XML to test your classes stored at the root of your test project with the Copy To Output Directory property set to Copy If Newer. You could use any path to create your ExeConfigurationFileMap, but for me putting the file at the root seemed the easiest solution.

image

Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • DZone
  • Digg
  • Google Bookmarks
  • Ma.gnolia
  • Technorati
hello
Categories
Uncategorized
Comments rss
Comments rss
Trackback
Trackback

« .NET Configuration – THE article on customizing the configuration of your app nServiceBus Management Extensions – new CodePlex project »

blog comments powered by Disqus

Subscribe

Calendar

May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Recent Posts

  • Lucy in the Sky with Ruby
  • Simple ASP.NET MVC Ajax Proxy
  • The incredible, fallible symmetric Unit Test, and the beauty of Open Source software.
  • You Can’t Fill an Imaginary Hole
  • I don’t know but I’ve been told, ETL is gettin’ mighty old. BAM! BAM! EDA! I want my data right away!

Recent Comments

  • Kim Won on SOA, ESBs and JBOWS, oh my!
  • AbdirahmanDaud on New Open Source .NET CMS/EPS Platform Released Today: Sense/Net 6.0 Beta 1
  • None on Rinsing the SOAP from WCF (or, RESTful WCF Hyperlink Acupuncture)
  • chill71uk on Simple ASP.NET MVC Ajax Proxy
  • mrhookah on Rule Based Access Control using an Expression Evaluator

Tags

TDD Testing

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox