問題
を取得する_mailSettings.Smtp.Network.Host
と、アプリケーション構成ファイルに含まれていても、が返されnull
ます。
コード
何よりもまず、プロジェクトは単なるであるClass Library
ため、適切な名前の構成ファイルをビルドするために、次のビルド後のイベントがあります。
copy "$(ProjectDir)App.config" "$(TargetDir)ApplicationSupport.Email.dll.config"
次に、構成をロードして使用可能にすることを目的としたクラスがあります。
public class Startup
{
SmtpSection _smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
public string SmtpServer
{
get
{
if (_smtpSection == null) { return string.Empty; }
return _smtpSection.Network.Host;
}
}
...
}
そして私はそれをユニットテストしようとしています:
[TestMethod]
public void TestStartupSettings()
{
var sut = new ApplicationSupport.Email.Startup();
Assert.AreEqual("test.mail.com", sut.SmtpServer);
}
アプリケーション構成ファイルでこれを構成しているので、データを取得する必要があると思いました。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<mailSettings>
<smtp from="somebody@test.com">
<network host="test.mail.com" defaultCredentials="true" port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>