Hi
So I have some SMTP stuff in my code and I am trying to unit test that method.
So I been trying to Mockup MailMessage but it never seems to work. I think none of the methods are virtual or abstract so I can't use moq to mock it up :(.
So I guess I have to do it by hand and that's where I am stuck.
*by hand I mean witting the interface and the wrapper but letting moq still mockup the interface.
I don't know how to write my Interface and my Wrapper(a class that will implement the interface that will have the actual MailMessage code so when my real code runs it actually does the stuff it needs to do).
So first I am not sure how to setup my Interface. Lets take a look at one of the fields that I have to mockup.
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
so this is the first thing that I have to fake.
so looking at it I know that "To" is a property by hitting F12 over "To" it takes me to this line:
public MailAddressCollection To { get; }
So it is MailAddressCollection Property. But some how I am allowed to go further and do "Add".
So now my question is in my interface what do I make?
do I make a property? Should this Property be MailAddressCollection?
Or should I have a method like?
void MailAddressCollection To(string email);
or
void string To.Add(string email);
Then how would my wrapper look?
So as you can see I am very confused. Since there is so many of them. I am guessing I just mockup the ones I am using.
edit code
I guess in in a true sense I would only have to test more the exceptions but I want to test to make sure if everything gets sent then it will get to response = success.
string response = null;
try
{
MembershipUser userName = Membership.GetUser(user);
string newPassword = userName.ResetPassword(securityAnswer);
MailMessage mail = new MailMessage();
mail.To.Add(userName.Email);
mail.From = new MailAddress(ConfigurationManager.AppSettings["FROMEMAIL"]);
mail.Subject = "Password Reset";
string body = userName + " Your Password has been reset. Your new temporary password is: " + newPassword;
mail.Body = body;
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["FROMPORT"]);
smtp.Send(mail);
response = "Success";
}
catch (ArgumentNullException ex)
{
response = ex.Message;
}
catch (ArgumentException ex)
{
response = ex.Message;
}
catch (ConfigurationErrorsException ex)
{
response = ex.Message;
}
catch (ObjectDisposedException ex)
{
response = ex.Message;
}
catch (InvalidOperationException ex)
{
response = ex.Message;
}
catch (SmtpFailedRecipientException ex)
{
response = ex.Message;
}
catch (SmtpException ex)
{
response = ex.Message;
}
return response;
}
Thanks