An alternative way to do this is to create a wrapper around SmtpClient that implements the same interface. Then paste and use the wrapper in your class. When doing unit testing, you can then replace the mock shell that has expectations for method calls and responses.
EDIT . A shell is required (for RhinoMocks, at least), because SmtpClient is not derived from the interface and does not have virtual methods. If you use a fake framework that can mock a class without virtual methods directly, you can skip the shell and immediately enter the SmtpClient layout.
public class SmtpClientWrapper { private SmtpClient Client { get; set; } public SmtpClientWrapper( SmtpClient client ) { this.Client = client; } public virtual void Send( MailMessage msg ) { this.Client.Send( msg ); } ... } public class MyClass { private SmtpClientWrapper Client { get; set; } public MyClass( SmtpClientWrapper client ) { this.Client = client; } public void DoSomethingAndNotify() { ... this.Client.Send( msg ); } }
Tested (with RhinoMocks) as:
public void DoSomethingAndNotifySendsAMessageTest() { SmtpClientWrapper client = MockRepository.GenerateMock<SmtpClientWrapper>(); client.Expect( c => c.Send( new MailMessage() ) ).IgnoreArguments(); MyClass klass = new MyClass( client ); klass.DoSomethingAndNotify(); client.VerifyAllExpectations(); }
tvanfosson Feb 15 '09 at 13:57 2009-02-15 13:57
source share