Unittest Projectのapp.configをhostType(「moles」)からテストから読む方法

StackOverflow https://stackoverflow.com/questions/4105604

  •  29-09-2019
  •  | 
  •  

質問

私には従来のテストがあります:

[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")]. 。しかし、最初のパスと2番目のパスは失敗します。 2番目のテストから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;
                };
    }

私は職場でこの問題に出くわしましたが、これらの答えのどれも気に入らなかった。また、構成ファイルが静的コンストラクターで読み取られているという問題もあります。

私はこれを自宅のコンピューターで試しましたが、構成ファイルが正しく読み取られていることがわかりました。自宅で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