How To Read UnitTest Project's App.Config From Test With HostType(“Moles”)

后端 未结 6 1446
夕颜
夕颜 2021-02-13 23:02

I have the folowing tests:

[TestClass]
public class GeneralTest
{
    [TestMethod]
    public void VerifyAppDomainHasConfigurationSettings()
    {
        string         


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 23:51

    This is what I am using to get the correct AppConfig and ConnectionString sections:

    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;
    

    Saw the ConnectionString part here

提交回复
热议问题