Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何阅读UnitTest项目';使用主机类型(“Moles”)测试的s App.Config_C#_Unit Testing_Mstest_Moles - Fatal编程技术网

C# 如何阅读UnitTest项目';使用主机类型(“Moles”)测试的s App.Config

C# 如何阅读UnitTest项目';使用主机类型(“Moles”)测试的s App.Config,c#,unit-testing,mstest,moles,C#,Unit Testing,Mstest,Moles,我有以下测试: [TestClass] public class GeneralTest { [TestMethod] public void VerifyAppDomainHasConfigurationSettings() { string value = ConfigurationManager.AppSettings["TestValue"]; Assert.IsFalse(String.IsNullOrEmpty(value),

我有以下测试:

[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

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

请参见


同时,作为一种解决方法,您可以尝试将配置设置添加到Microsoft.Moles.VsHost.x86.exe.config中。我在工作中遇到了这个问题,不喜欢这些答案。我还有一个问题,就是在静态构造函数中读取配置文件,这意味着在执行静态构造函数之前,我无法启动ConfigurationManager

    [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;
                };
    }
我在家里的电脑上试过,发现配置文件被正确读取。原来我在家里用的是Pex 0.94.51006.1。这比当前版本稍旧。我能够找到旧学术版本的下载:


我把它安装在我的工作电脑上,一切都很正常。此时,我将降级到旧版本,直到发布新的工作版本。

假设您尝试访问appSettings中的值,那么在测试开始时添加配置如何。比如:

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

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

这就是我用来获取正确的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部分,您只需将“App.Config”文件添加到单元测试项目中。它将自动读取。

根据链接,这是一个错误,正在处理中。只是为了让ppl不必去那里看帖子。这真的很糟糕。我们有一些集成测试,其中(无论好坏)测试app.config实际上相当大。我无法重新实现ConfigurationManager/WebConfigurationManager.com,但如果我使用它来获取应用设置,则会发生以下错误:“System.Configuration.ConfigurationErrorsException:配置部分“appSettings”具有意外声明”。是否对此进行了修复?@Dennis,若要解决此错误,请参阅此处的示例代码:如果没有任何自定义appSettings,您只需“return new NameValueCollection();”欢迎使用Stack Overflow。这是一个问答网站,不是一个典型的论坛,所以把一个问题作为你答案的一部分,不会得到适当的关注,也不适合这个网站,因为一个问题的答案应该只针对这个问题。通常我会建议您单独发布您的问题,但询问工具是否仍在开发中是离题的。这是一个最好向工具开发者提出的问题。谢谢。对于我类似的问题,我提出了一个很好的解决办法:我发现这个答案:这是在使用moles时更改配置文件的最佳方法。如果您使用自定义配置部分,它会工作得很好(实际上,它是唯一一个在这种情况下工作的部分)。如果由我决定的话,我会让这成为公认的答案!!!非常感谢你。这真是一个无聊的时刻。Lolz只是想向任何感兴趣的人添加一个连接字符串,但添加一个连接字符串有点不同:ConfigurationManager.ConnectionStrings.add(newconnectionstringsettings(“name”,“connectionstring”);做了一顿款待。谢谢,Kipps有时最简单的解决方案是最好的-这对我来说很有效,而不是所有的OpenExeConfiguration之类的虚张声势。。。