Вы также можете указать его для вашего проекта unittest:
public enum TestContextKeys { EmailPickupDirectory, ... };
[TestClass]
public class AssemblyInitializer
{
[AssemblyInitialize]
public static void Init(TestContext testContext)
{
string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
XDocument xmlConfig = XDocument.Load(configPath);
var emailPickupDirectory = xmlConfig.Element("configuration")
.Element("system.net")
.Element("mailSettings")
.Element("smtp")
.Element("specifiedPickupDirectory")
.Attribute("pickupDirectoryLocation")
.Value;
testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()] = emailPickupDirectory;
}
И ваш тестовый код:
[TestMethod]
public void TestEmailRegistration()
{
MyApp app = new MyApp();
app.RegisterUser("Johny Cash",...);
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
//check that the registration email was sent
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains("Johny Cash") && content.Contains("successful") && content.Contains("registration"))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("Registratoin email has not been sent to Johny Cash");
}
[TestMethod]
public void TestEmailPickupDirectoryConfiguration()
{
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
MailAddress mailFrom = new MailAddress("testemailpickupdirectory@example.com", "Tester");
MailAddress mailTo = new MailAddress("testemailpickupdirectory@testing.com", "Tester2");
string subject = "Test Message TestEmailPickupDirectory";
using (SmtpClient sc = new SmtpClient())
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(mailTo);
mail.Subject = subject;
mail.From = mailFrom;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "TestEmailPickupDirectory";
mail.Body += "</body></html>";
sc.Send(mail);
}
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains(mailFrom.Address) && content.Contains(mailTo.Address) && content.Contains(subject))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("EmailPickupDirectory configuration may be wrong.");
}
Создайте файл app.config в вашем проекте unittest, если он не существует, или объедините эти строки с существующим app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="d:\temp\Emails\" />
</smtp>
</mailSettings>
</system.net>
</configuration>