How To Read UnitTest Project's App.Config From Test With HostType(“Moles”)

后端 未结 6 1455
夕颜
夕颜 2021-02-13 23:02

I have the folowing tests:

[TestClass]
public class GeneralTest
{
    [TestMethod]
    public void VerifyAppDomainHasConfigurationSettings()
    {
        string         


        
相关标签:
6条回答
  • 2021-02-13 23:29

    Assuming you are trying to access values in appSettings, how about just adding the configuration at the beginning of your test. Something like:

    ConfigurationManager.AppSettings["Key"] = "Value";
    

    Then when your test tries to read the AppSettings "Key", "Value" will be returned.

    0 讨论(0)
  • 2021-02-13 23:34

    See http://social.msdn.microsoft.com/Forums/en/pex/thread/9b4b9ec5-582c-41e8-8b9c-1bb9457ba3f6

    In the mean time, as a work around, you could try adding the configuration settings to Microsoft.Moles.VsHost.x86.exe.config

    0 讨论(0)
  • 2021-02-13 23:40
        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            System.Configuration.Moles.MConfigurationManager.GetSectionString =
                (string configurationName) =>
                    {
                        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                        Assembly assembly = Assembly.GetExecutingAssembly();
                        fileMap.ExeConfigFilename = assembly.Location + ".config";
                        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                        object section = config.GetSection(configurationName);
                        if (section is DefaultSection)
                        {
                            ConfigurationSection configurationSection = (ConfigurationSection) section;
                            Type sectionType = Type.GetType(configurationSection.SectionInformation.Type);
                            if (sectionType != null)
                            {
                                IConfigurationSectionHandler sectionHandler =
                                    (IConfigurationSectionHandler)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(sectionType.Assembly.FullName, sectionType.FullName);
                                section = 
                                    sectionHandler.Create(
                                        configurationSection.SectionInformation.GetParentSection(), 
                                        null,
                                        XElement.Parse(configurationSection.SectionInformation.GetRawXml()).ToXmlNode());
                            }
                        }
    
                        return section;
                    };
        }
    
    0 讨论(0)
  • 2021-02-13 23:40

    I ran across this issue at work and didn't like any of these answers. I also have the problem that the configuration file is being read in a static constructor which means I can't Mole ConfigurationManager before the static constructor is executed.

    I tried this on my home computer and found that the configuration file was being read correctly. It turns out I was using Pex 0.94.51006.1 at home. This is slightly older than the current one. I was able to find a download for the older academic version: http://research.microsoft.com/en-us/downloads/d2279651-851f-4d7a-bf05-16fd7eb26559/default.aspx

    I installed this on my work computer and everything is working perfectly. At this point, I'm downgrading to the older version until a newer working version is released.

    0 讨论(0)
  • 2021-02-13 23:51

    This is what I am using to get the correct AppConfig and ConnectionString sections:

    var config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Reflection.Assembly.GetExecutingAssembly().Location);
    
    typeof(Configuration.ConfigurationElementCollection).GetField("bReadOnly", Reflection.BindingFlags.Instance | Reflection.BindingFlags.NonPublic).SetValue(System.Configuration.ConfigurationManager.ConnectionStrings, false);
    foreach (Configuration.ConnectionStringSettings conn in config.ConnectionStrings.ConnectionStrings)
        System.Configuration.ConfigurationManager.ConnectionStrings.Add(conn);
    
    foreach (Configuration.KeyValueConfigurationElement conf in config.AppSettings.Settings)
        System.Configuration.ConfigurationManager.AppSettings(conf.Key) = conf.Value;
    

    Saw the ConnectionString part here

    0 讨论(0)
  • 2021-02-13 23:53

    You just add your "App.Config" file to the unit test project . It will read automatically.

    0 讨论(0)
提交回复
热议问题