你如何单元测试邮件发送功能 [英] How do you unit test a mail sending function

查看:840
本文介绍了你如何单元测试邮件发送功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它创建了一个报告,并将其发送给用户的功能。
我所做的就是创建一个电子邮件功能的模拟,并确认该邮件类的发送函数被调用。

i have a function which creates a report and sends it to a user. What i've done is create a mock for the email function, and verify if the 'send' function of the email class was called.

所以,现在我知道,函数被调用,但你如何单元测试的send()函数的主体?我怎样才能坡口的主题和正文都是正确和附件附加到电子邮件?

So now i know that the function is called, but how do you unit test the body of the Send() function? How can i proove that the subject and body are correct and an attachment is attached to the email?

米歇尔

推荐答案

可通过以下方式进行

第1步:找到的Web.Config 文件和以下标签添加到它

Step 1: Navigate to your Web.Config file and add the following tags to it.

<system.net>
    <mailSettings>
        <smtp deliveryMethod="SpecifiedPickupDirectory">
            <specifiedPickupDirectory pickupDirectoryLocation="E:\MailTest\"/>
        </smtp>
    </mailSettings>
</system.net>

请确保您已为皮卡位置指定的目录必须存在。

Make sure the directory you have specified for pickup location must exist.

第2步:现在,测试你的电子邮件发送功能。我已经使用 button_click 来测试用下面的代码此功能。

Step 2 : Now test your email sending functionality. I have used button_click to test this functionality with the following code.

SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage("me@gmail.com", "me@yahoo.com","My Message Subject","This is a test message");
smtp.Send(message);



输出:它将创建的.eml 文件用randonly生成的GUID的名字,这是我们可以在收到后看到电子邮件中的文件夹内。
对我来说,创造了电子像 c127d1d5-255d-4a5a-873c-409e23002eef.eml 文件:\MailTest\ 文件夹

Output : It will create .eml files inside the folder with a randonly generated GUID name, which is the email that we can see after receiving it. For me it created a file like c127d1d5-255d-4a5a-873c-409e23002eef.eml in E:\MailTest\ folder

希望这有助于:)

这篇关于你如何单元测试邮件发送功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆