Testing web configuration from a class library

I save my application settings in my web.config file and access them using ConfigurationManager.GetSection or ConfigurationManager.AppSettings. I created a test to ensure that the parameters I am getting from my class are the correct parameters in the web.config file, however it returns null values. How can I tell the unit test that the class library should use the web.config file for this configuration and not the app.config file that accompanies the class library? In this case, since I am creating a quick test, I am using the VS built-in unit test library.

+2


source to share


3 answers


I was able to add an app.config file to my Tests app to fix the problem.



0


source


Quite long, but try this:

using System;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.Xml.Serialization;

namespace Sol3.Configuration
{
    //######################################################################################################################################################
    /// <summary>
    /// This creates a configuration section handler letting the system know that any object with a 
    /// typeof(ClientConfiguration) can tie directly to the config file and section.
    /// </summary>
    public class ClientSetupSerializerSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Sol3.Configuration.ClientSetupConfiguration));

            return ser.Deserialize(new XmlNodeReader(section));
        }
    }
    //######################################################################################################################################################
    /// <summary>
    /// This is the concrete class that will handle the "ClientHandler" section in the web.config.
    /// </summary>
    [Serializable()]
    [XmlRoot("BasicSetup")]
    public class ClientSetupConfiguration
    {
        #region Properties
        [XmlElement("HomePageRedirect")] public string HomePageRedirect;
        [XmlElement("ServicesRootUrl")] public string ServicesRootUrl;
        [XmlElement("CookieDomain")] public string CookieDomain;
        [XmlElement("AllowScriptAccess")] public string AllowScriptAccess;
...
        [XmlElement] public string Info_Server;
        [XmlElement] public string Info_Database;
        #endregion

        #region Static Methods
        public static ClientSetupConfiguration GetSetupConfig()
        {
            if (HttpContext.Current.Cache["ClientSetupConfiguration"] == null)
                HttpContext.Current.Cache.Insert("ClientSetupConfiguration", ConfigurationManager.GetSection("BasicSetup"));

            return (Sol3.Configuration.ClientSetupConfiguration)HttpContext.Current.Cache["ClientSetupConfiguration"];
        }
        #endregion
    }
}

      

Then I created a helper class that looks like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sol3    {
    static class AppSettings
    {
        public static string Info_Server { get { return Sol3.Configuration.ClientSetupConfiguration.GetSetupConfig().Info_Server; } }
        public static string Info_Database { get { return Sol3.Configuration.ClientSetupConfiguration.GetSetupConfig().Info_Database; } }
...
        static public bool inProduction { get { return Info_Server.ToLower().Contains("rackspace"); } }

    }
}

      



Now, from anywhere in the code, I can call Sol3.AppSettings.Info_Server and it gets this information from the web.config.

All this is done in a DLL project and has been working for us for quite some time now. GetSetupConfig () is key as it knows how to get the web.config file ...

NTN

0


source


In the app.config file, you can add the "file" attribute to the appSettings element:

<appSettings file = "d: \ mydir \ web.config">

I do not endorse this as a best practice, but it will force your app.config to inherit your web.config's application settings.

-2


source







All Articles