12

我有以下测试:

[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?

或者我可以在其他地方添加一些其他配置文件?

4

6 回答 6

17

假设您正在尝试访问 appSettings 中的值,那么在测试开始时添加配置怎么样。就像是:

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

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

于 2011-12-31T06:27:39.530 回答
12

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

于 2012-10-18T06:45:47.070 回答
6

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

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

于 2010-12-08T08:36:11.513 回答
5
    [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;
                };
    }
于 2011-02-12T23:15:55.090 回答
2

我在工作中遇到了这个问题,不喜欢这些答案中的任何一个。我还有一个问题是在静态构造函数中读取配置文件,这意味着在执行静态构造函数之前我不能 Mole ConfigurationManager。

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

我将它安装在我的工作计算机上,一切正常。在这一点上,我正在降级到旧版本,直到发布更新的工作版本。

于 2011-12-31T01:39:37.553 回答
0

这就是我用来获取正确 AppConfig 和 ConnectionString 部分的方法:

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;

在这里看到了 ConnectionString 部分

于 2012-02-02T16:49:09.527 回答