Created
June 6, 2011 05:30
-
-
Save amirci/1009777 to your computer and use it in GitHub Desktop.
Virtual and redefining methods with "new" is not the same
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConcreteMockTest | |
{ | |
[Test] | |
public void Worker_WhenDoWorkIsCalled_SendsEmailToTylerAboutRussianBrides() | |
{ | |
var emailSender = new FakeEmailSender(); | |
var worker = new Worker(emailSender); | |
worker.DoWork(); | |
var message = emailSender.SentMessages.Last(); | |
Assert.AreEqual("Russian brides!", message.Body); | |
Assert.AreEqual("[email protected]", message.To[0].Address); | |
} | |
} | |
class FakeEmailSender : EmailSender | |
{ | |
public FakeEmailSender() | |
{ | |
this.SentMessages = new List<MailMessage>(); | |
} | |
new public void SendEmail(string toAddress, string body) | |
{ | |
var m = new MailMessage("[email protected]", toAddress, "spam", body); | |
SentMessages.Add(m); | |
} | |
public ICollection<MailMessage> SentMessages { get; private set; } | |
} | |
class Worker | |
{ | |
private readonly EmailSender _notifier; | |
public Worker(EmailSender notifier) | |
{ | |
_notifier = notifier; | |
} | |
public void DoWork() | |
{ | |
_notifier.SendEmail("[email protected]", "Russian brides!"); | |
} | |
} | |
class EmailSender | |
{ | |
public void SendEmail(string toAddress, string body) | |
{ | |
var m = new MailMessage("[email protected]", toAddress, "EMAILZ!", body); | |
var client = new SmtpClient(); | |
client.Send(m); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment