我有效果测试:

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

还是我可以在其他地方添加其他配置文件?

有帮助吗?

解决方案

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

同时,作为工作,您可以尝试将配置设置添加到microsoft.moles.vshost.x86.exe.config

其他提示

假设您正在尝试在AppSettings中访问值,那么如何在测试开始时添加配置。就像是:

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

然后,当您的测试试图读取AppSettings“键”时,将返回“值”。

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

    [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;
                };
    }

我在工作中遇到了这个问题,不喜欢这些答案。我还遇到了一个问题,即在静态构造函数中读取配置文件,这意味着在执行静态构造函数之前,我无法mole ConfigurationManager。

我在家用计算机上尝试了此操作,发现配置文件正在正确读取。事实证明,我在家中使用了PEX 0.94.51006.1。这比当前的年龄稍大。我能够找到较旧的学术版本的下载:http://research.microsoft.com/en-us/downloads/d2279651-851f-4d7a bf05-16fd7eb26559/default.aspx

我将其安装在工作计算机上,一切正常。此时,我要降级到较旧版本,直到发布了更新的工作版本为止。

这就是我用来获得正确的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;

看到连接串件 这里

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top