代码之家  ›  专栏  ›  技术社区  ›  Draco Ater

如何从Test With HostType(“Moles”)读取UnitTest项目的App.Config

  •  12
  • Draco Ater  · 技术社区  · 14 年前

    [TestClass]
    public class GeneralTest
    {
        [TestMethod]
        public void VerifyAppDomainHasConfigurationSettings()
        {
            string value = ConfigurationManager.AppSettings["TestValue"];
            Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
        }
    
        [TestMethod]
        [HostType("Moles")]
        public void VerifyAppDomainHasConfigurationSettingsMoles()
        {
            string value = ConfigurationManager.AppSettings["TestValue"];
            Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
        }
    }
    

    他们之间唯一的区别是 [HostType("Moles")] . 但第一次通过,第二次失败。如何从第二次测试中读取App.config?

    6 回复  |  直到 14 年前
        1
  •  6
  •   Andrew Cupper    14 年前

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

    同时,作为解决方法,您可以尝试将配置设置添加到Microsoft.Moles.VsHost.x86.exe.config

        2
  •  17
  •   Kipp    13 年前

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

    然后,当您的测试尝试读取AppSettings“Key”时,将返回“Value”。

        3
  •  12
  •   Shanthi Gopalan    12 年前

    您只需将“App.Config”文件添加到单元测试项目。它会自动读取。

        4
  •  5
  •   Robin H. Sanner    14 年前
        [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;
                    };
        }
    
        5
  •  2
  •   Adam Lear    13 年前

    我在家里的电脑上试了一下,发现配置文件被正确读取了。原来我在家里用的是Pex 0.94.51006.1。这个比现在的稍微旧一点。我找到了旧学术版的下载: http://research.microsoft.com/en-us/downloads/d2279651-851f-4d7a-bf05-16fd7eb26559/default.aspx

    我把这个安装在我的工作电脑上,一切都正常。在这一点上,我正在降级到旧版本,直到新的工作版本发布。

        6
  •  0
  •   Community Patrick Maupin    7 年前

    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;
    

    看到连接字符串部分 here