Frage

Ich habe die Folowing Tests:

[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.");
    }
}

Der einzige Unterschied zwischen ihnen ist [HostType("Moles")]. Aber die ersten Pässe und das zweite nicht. Wie kann ich App.config aus dem zweiten Test lesen?

Oder kann ich eine andere Konfigurationsdatei an anderer Stelle hinzufügen?

War es hilfreich?

Lösung

Siehe http: // Sozial .msdn.microsoft.com / Foren / en / PEX / thread / 9b4b9ec5-582c-41e8-8b9c-1bb9457ba3f6

In der Zwischenzeit, als eine Arbeit um, Sie könnten versuchen, die Konfigurationseinstellungen zu Microsoft.Moles.VsHost.x86.exe.config Hinzufügen

Andere Tipps

Angenommen, Sie versuchen, den Zugriff auf Werte in appSettings, wie etwa dem Hinzufügen nur die Konfiguration zu Beginn des Tests. So etwas wie:

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

Dann, wenn der Test versucht, die AppSettings „Key“ zu lesen, „Value“ wird zurückgegeben.

Sie haben soeben Ihre „App.Config“ Datei an das Gerät Testprojekt hinzufügen. Es wird automatisch gelesen.

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

lief ich über dieses Thema bei der Arbeit und tat es nicht wie eine dieser Antworten. Ich habe auch das Problem, dass die Konfigurationsdatei in einem statischen Konstruktor gelesen werden, was bedeutet, ich kann nicht Mole Configuration bevor der statische Konstruktor ausgeführt wird.

Ich habe versucht, diese auf meinem Computer zu Hause und stellte fest, dass die Konfigurationsdatei korrekt gelesen wurde. Es stellt sich heraus, ich war Pex 0.94.51006.1 zu Hause. Dies ist etwas älter als die aktuelle. Ich konnte einen Download für die ältere akademische Version finden: http://research.microsoft.com /en-us/downloads/d2279651-851f-4d7a-bf05-16fd7eb26559/default.aspx

ich dies installiert auf meinem Computer und alles funktioniert perfekt. An diesem Punkt ist ich Herabstufung auf die ältere Version, bis eine neue Arbeits Version freigegeben wird.

Das ist, was ich mit dem richtigen AppConfig und Connection Abschnitten zu erhalten:

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;

Sehen die Connection Teil hier

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top