Riffing от @codekaizen, используя AutoFixture.xUnit [который доступен как пакет XUnit с таким именем]: -
[Theory, AutoData]
public static void ShouldSendWithCorrectValues( string anonymousFrom, string anonymousRecipients, string anonymousSubject, string anonymousBody )
{
anonymousFrom += "@b.com";
anonymousRecipients += "@c.com";
using ( var tempDir = new TemporaryDirectoryFixture() )
{
var capturingSmtpClient = new CapturingSmtpClientFixture( tempDir.DirectoryPath );
var sut = new EmailSender( capturingSmtpClient.SmtpClient );
sut.Send( anonymousFrom, anonymousRecipients, anonymousSubject, anonymousBody );
string expectedSingleFilename = capturingSmtpClient.EnumeratePickedUpFiles().Single();
var result = File.ReadAllText( expectedSingleFilename );
Assert.Contains( "From: " + anonymousFrom, result );
Assert.Contains( "To: " + anonymousRecipients, result );
Assert.Contains( "Subject: " + anonymousSubject, result );
Assert.Contains( anonymousBody, result );
}
}
CapturingSmtpClientFixture
используется только в тестовом контексте-
class CapturingSmtpClientFixture
{
readonly string _path;
readonly SmtpClient _smtpClient;
public CapturingSmtpClientFixture( string path )
{
_path = path;
_smtpClient = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory, PickupDirectoryLocation = _path };
}
public SmtpClient SmtpClient
{
get { return _smtpClient; }
}
public IEnumerable<string> EnumeratePickedUpFiles()
{
return Directory.EnumerateFiles( _path );
}
}
Все, что вам нужно сделать, это убедиться, что ваш фактический код содержит SmtpClient
, который был связан с параметрами, соответствующими действующему SMTP-серверу.
(для полноты здесь TemporaryDirectoryFixture
): -
public class TemporaryDirectoryFixture : IDisposable
{
readonly string _directoryPath;
public TemporaryDirectoryFixture()
{
string randomDirectoryName = Path.GetFileNameWithoutExtension( Path.GetRandomFileName() );
_directoryPath = Path.Combine( Path.GetTempPath(), randomDirectoryName );
Directory.CreateDirectory( DirectoryPath );
}
public string DirectoryPath
{
get { return _directoryPath; }
}
public void Dispose()
{
try
{
if ( Directory.Exists( _directoryPath ) )
Directory.Delete( _directoryPath, true );
}
catch ( IOException )
{
// Give other process a chance to release their handles
// see /307725/nevozmozhno-udalit-katalog-s-pomoschy-directory-delete-put-istina#307751
Thread.Sleep( 0 );
try
{
Directory.Delete( _directoryPath, true );
}
catch
{
var longDelayS = 2;
try
{
// This time we'll have to be _really_ patient
Thread.Sleep( TimeSpan.FromSeconds( longDelayS ) );
Directory.Delete( _directoryPath, true );
}
catch ( Exception ex )
{
throw new Exception( @"Could not delete " + GetType() + @" directory: """ + _directoryPath + @""" due to locking, even after " + longDelayS + " seconds", ex );
}
}
}
}
}
и скелет EmailSender
:
public class EmailSender
{
readonly SmtpClient _smtpClient;
public EmailSender( SmtpClient smtpClient )
{
if ( smtpClient == null )
throw new ArgumentNullException( "smtpClient" );
_smtpClient = smtpClient;
}
public void Send( string from, string recipients, string subject, string body )
{
_smtpClient.Send( from, recipients, subject, body );
}
}