設定値に依存する機能をテストしようとしています(Settings["foo"]
= trueの場合は5、そうでない場合は-1を返します)。tetsプロジェクトのapp.configファイルの設定値を変更する
私がしようとしているのは、実行時に設定値を変更することです。
私の設定ファイルは、そう(簡体字)のようになります。
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="DomainSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<applicationSettings>
<DomainSettings>
<setting name="foo" serializeAs="String">
<value>false</value>
</setting>
</ICTS.SmartQueue.Domain.DomainSettings>
</applicationSettings>
と私は次のことをやっている:
//get config file
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//get relevant section
var section = (ClientSettingsSection)config.GetSection("applicationSettings/DomainSettings");
//get element from section
var element = section.Settings.Get("Foo");
//change its value and save it
element.Value.ValueXml.InnerText = true.ToString();
config.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
//force refresh
ConfigurationManager.RefreshSection("applicationSettings/DomainSettings");
私はテストのconfigを見たときに値が実際に変更されていることがわかりますファイルを 'Out'ディレクトリ(MyTests.DLL.config)に保存します。
ただし、DomainSettings.Default.Foo
は「偽」と評価されます。
私は、 'ConfigurationManager.RefreshSection'がそれを処理するべきだと考えました。 (MSDNから: "このメソッドは、他のセクションに影響を与えずに指定された構成セクションのキャッシュを無効にします。") –