因此,我的代码中有一些SMTP内容,我正在尝试对该方法进行单元测试。
所以我一直在尝试模拟MailMessage,但它似乎从来都不起作用。我认为这些方法都不是虚拟的或抽象的,所以我不能使用moq来模拟它:(.
所以我想我不得不手工完成,这就是我被困住的地方。
*手动我的意思是知道界面和包装器,但让moq仍然模拟界面。
我不知道如何编写我的接口和包装器(一个实现具有实际MailMessage代码的接口的类,因此当我的实际代码运行时,它实际上做了它需要做的事情)。
所以首先我不确定如何设置我的界面。让我们看一看我必须模拟的一个字段。
MailMessage mail = new MailMessage();
mail.To.Add("test@hotmail.com");
所以这是我要伪造的第一件事。
看着它,我知道" to“是一个属性,只需在"To”上点击F12,就可以转到下面这一行:
public MailAddressCollection To { get; }
所以它是MailAddressCollection属性。而是如何允许我走得更远,并做“添加”。
所以现在我的问题是在我的界面中我做了什么?
我做了一个属性吗?此属性是否应为MailAddressCollection?
或者我应该有一个像这样的方法?
void MailAddressCollection To(string email);
or
void string To.Add(string email);
那么我的包装器看起来会怎么样呢?
如你所见,我非常困惑。因为他们太多了。我猜我只是模拟了我正在使用的那些。
编辑代码
我猜在真正的意义上,我只需要测试更多的异常,但我想测试以确保如果一切都被发送了,那么它将得到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;
}
谢谢
发布于 2009-06-29 12:52:13
为什么要嘲笑MailMessage呢?SmtpClient接收MailMessages并将其发送出去;这是我出于测试目的而希望包装的类。因此,如果您正在编写某种类型的下单系统,如果您正在尝试测试您的OrderService在下单时是否始终发送电子邮件,您将拥有一个类似于以下内容的类:
class OrderService : IOrderSerivce
{
private IEmailService _mailer;
public OrderService(IEmailService mailSvc)
{
this. _mailer = mailSvc;
}
public void SubmitOrder(Order order)
{
// other order-related code here
System.Net.Mail.MailMessage confirmationEmail = ... // create the confirmation email
_mailer.SendEmail(confirmationEmail);
}
}
使用IEmailService包装SmtpClient的默认实现:
这样,当您编写单元测试时,您测试的是使用SmtpClient/EmailMessage类的代码的行为,而不是SmtpClient/EmailMessage类本身的行为:
public Class When_an_order_is_placed
{
[Setup]
public void TestSetup() {
Order o = CreateTestOrder();
mockedEmailService = CreateTestEmailService(); // this is what you want to mock
IOrderService orderService = CreateTestOrderService(mockedEmailService);
orderService.SubmitOrder(o);
}
[Test]
public void A_confirmation_email_should_be_sent() {
Assert.IsTrue(mockedEmailService.SentMailMessage != null);
}
[Test]
public void The_email_should_go_to_the_customer() {
Assert.IsTrue(mockedEmailService.SentMailMessage.To.Contains("test@hotmail.com"));
}
}
编辑:为了解决下面的问题,您需要两个独立的EmailService实现--只有一个将使用SmtpClient,您将在应用程序代码中使用它:
class EmailService : IEmailService {
private SmtpClient client;
public EmailService() {
client = new SmtpClient();
object settings = ConfigurationManager.AppSettings["SMTP"];
// assign settings to SmtpClient, and set any other behavior you
// from SmtpClient in your application, such as ssl, host, credentials,
// delivery method, etc
}
public void SendEmail(MailMessage message) {
client.Send(message);
}
}
您的模拟/伪造电子邮件服务(您不需要模拟框架,但它很有帮助)不会接触到SmtpClient或SmtpSettings;它只会记录在某个时刻,通过SendEmail向它传递了一封电子邮件的事实。然后,您可以使用它来测试是否调用了SendEmail,以及使用了哪些参数:
class MockEmailService : IEmailService {
private EmailMessage sentMessage;;
public SentMailMessage { get { return sentMessage; } }
public void SendEmail(MailMessage message) {
sentMessage = message;
}
}
是否将电子邮件发送到SMTP服务器并交付的实际测试应该超出单元测试的范围。您需要知道这是否有效,并且可以设置第二组测试来专门测试这一点(通常称为集成测试),但这些测试与测试应用程序核心行为的代码是分开的。
发布于 2009-06-29 12:46:46
在这里,您将最终模拟几个不同的类(至少两个)。首先,您需要对MailMessage类进行包装。我将为包装器创建一个接口,然后让包装器实现该接口。在您的测试中,您将模拟接口。其次,您将提供一个模拟实现作为对MailAddressCollection的模拟接口的期望。因为MailAddressCollection实现了Collection<MailAddress>
,所以这应该相当简单。如果由于其他属性(我没有检查)而模拟MailAddressCollection是有问题的,那么可以让包装器将其作为IList<MailAddress>
返回,作为接口应该很容易模拟。
public interface IMailMessageWrapper
{
MailAddressCollection To { get; }
}
public class MailMessageWrapper
{
private MailMessage Message { get; set; }
public MailMessageWrapper( MailMessage message )
{
this.Message = message;
}
public MailAddressCollection To
{
get { return this.Message.To; }
}
}
// RhinoMock syntax, sorry -- but I don't use Moq
public void MessageToTest()
{
var message = MockRepository.GenerateMock<IMailMessageWrapper>()
var to = MockRepository.GenerateMock<MailAddressCollection>();
var expectedAddress = "test@example.com";
message.Expect( m => m.To ).Return( to ).Repeat.Any();
to.Expect( t => t.Add( expectedAddress ) );
...
}
https://stackoverflow.com/questions/1058041
复制相似问题