如何对电子邮件规则进行单元测试
我将员工的电子邮件下载到表中,然后获取刚刚下载的电子邮件集合并对它们运行一系列规则。
例如,如果电子邮件来自[电子邮件受保护],则将电子邮件路由到folder1。
我如何为这个过程编写单元测试?
我是否必须创建一个虚拟的电子邮件集合,然后为每封电子邮件制定正确的规则? 我很难打破单元测试,我总是想将数据库过程包含到测试中,但我知道这是错误的。
I download an employee's email into a table, I then take that collection of emails I just downloaded and run a bunch of rules on them.
e.g. if email from [email protected], route the email to folder1.
How could I write a unit test for this process?
Would I have to create a dummy collection of emails, and then right a rule for each one?
I have a hard time breaking the unit test out, I always want to include database procedures into the test which I know is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何测试一个软件很大程度上取决于如何将其分解为不同的组件。实际上没有对“过程”进行测试。您应该专注于单个组件。例如,使用您所描述的流程,该软件可以分解为不同的组件,如下所示:
您实际上将单独测试每个组件,运行以下测试:练习每个组件的各个方面(尽可能多)。
How you can test a piece of software largely depends how you've broken it down into different components. There's really no testing of a "process". You should be focusing on individual components instead. For example, with a process like the one you described, the software could be broken down into different components as follows:
You would essentially be testing each component individually, running tests that exercise every aspect (as much as possible) of each component.
单元测试的确切形式取决于系统的架构。通常,您会创建一组模拟电子邮件。使用模拟的电子邮件对象。它们不必实际存在于数据库中。无论如何,您应该将电子邮件表示为类。
编写单元测试以对模拟电子邮件执行操作,并断言该规则的行为符合您的预期。
The exact shape of your unit tests will depend on the architecture of your system. Generally, you would create a set of mock emails. Use a mocked Email object. They don't have to actually exist in a database. You should have the emails represented as classes anyway.
Write your unit tests to Act on the mocked emails, and assert that the rule behaved the way you expect.
我在这里是一个纯粹主义者,但单元测试应该测试最小的代码单元。这通常是一个类方法。因此,您应该查看您的一个或多个类拥有的每个公共方法,并编写仅测试该方法中的逻辑的测试。这可能意味着模拟其他交互的类,或者重构代码以减少依赖性。通常你会发现你有一个具有多种职责的类,应该将其分解为多个类,这样就可以为其编写单元测试。这就是单元测试是评估活动而不是测试活动的原因。它应该提高您的设计质量。
您还可以查看以客户为中心的测试,以测试所有功能的协同工作。
I'm being a purist here, but a unit test should test the smallest unit of code. Which is normally a class method. So you should look at each public method your class or classes has and write test which test only the logic in that method. That may mean mocking out other classes that interact, or refactoring your code to reduce the dependencies. Often you will find that you have a class that has multiple responsibilities which should be broken down into multiple classes which will make it possible to write unit tests for. This is how unit testing is a appraisal activity rather than a testing activity. It should increase the quality of your design.
You might also look at customer focused tests for testing it all works together.