本文共 6354 字,大约阅读时间需要 21 分钟。
打破双亲委派么,怎么打破
by Guido Schmitz
由Guido Schmitz
When I started writing code, I never wrote any tests. I assumed that my code didn’t contain any bugs. I sort of figured that everything would just keep on working when I changed a line of code here or there, or shipped new features entirely.
当我开始编写代码时,我从未编写过任何测试。 我以为我的代码没有任何错误。 我有点想,当我在这里或那里更改一行代码,或者完全交付新功能时,一切都将继续工作。
Boy, was I wrong.
男孩,我错了。
While my application remained functional, weird bugs began creeping in. And things just got worse as my codebase grew.
当我的应用程序保持功能正常时,奇怪的错误开始蔓延。随着我的代码库的增加,情况变得越来越糟。
Soon I got anxious whenever adding new code to my codebase, thinking my next line of code could be the one that brings the whole app crashing down.
很快,每当将新代码添加到我的代码库中时,我都会感到焦虑,认为我的下一行代码可能会使整个应用崩溃。
That’s when I discovered Test Driven Development (TDD).
那是我发现测试驱动开发(TDD)的时候。
TDD is a methodology that can increase your confidence when shipping new features. It makes you less likely to break your application.
TDD是一种可以在发布新功能时增加信心的方法。 它使您减少破坏应用程序的可能性。
When beginning with TDD there are you should follow:
从TDD开始时,应遵循以下 :
Then you can go in and refactor the production code you wrote.
然后,您可以继续并重构您编写的生产代码。
This process as a whole is often referred to as Red Light (failing test) -> Green Light (passing test) -> Refactor
整个过程通常称为红灯(失败测试)->绿灯(通过测试)->重构
By following these three rules, TDD can help you with:
通过遵循这三个规则,TDD可以帮助您:
Debugging. Imagine working on a project where you never end up with several modules torn to shreds, hoping that you can somehow pull them all back together by your deadline.
调试。 想象一下在一个项目中工作,您永远不会将几个模块撕成碎片,希望您能以某种方式在截止日期之前将它们重新组合在一起。
Courage. If you have a beautiful design and architecture, but have no tests, you are still afraid to change the code. At the same time, if you have a broad suite of tests, you can go back and safely refactor sub-par code.
勇气。 如果您拥有漂亮的设计和体系结构,但是没有测试,那么您仍然害怕更改代码。 同时,如果您有各种各样的测试,则可以返回并安全地重构低于标准价的代码。
Documentation. Unit tests are like code examples. When you want to know how to call a method, you’ll have tests handy that call that method every way it can be called, and these cannot get out of sync with your production code.
文档。 单元测试就像代码示例。 当您想知道如何调用方法时,您将拥有方便的测试,可以以每种方式调用该方法,并且这些测试不会与您的生产代码不同步。
Design. Each module will be testable by definition. And another word for testable is decoupled. In order to write your tests first, you have to decouple the units you are testing from the rest of the system. This practice is invaluable.
设计。 每个模块都可以根据定义进行测试 。 另一个可测试的词被解耦了 。 为了首先编写测试,必须将要测试的单元与系统的其余部分解耦。 这种做法是无价的。
Professionalism. Given that these benefits are real, the bottom line is that it would be unprofessional not to adopt the practice that yields them.
专业精神。 鉴于这些好处是真实的,最重要的是,不采用产生这些好处的做法是不专业的 。
Every test should follow a structure like this one:
每个测试都应遵循以下结构:
Setup: Mocking a function or adding some rows to your database
设置:模拟函数或向数据库中添加一些行
Execute: Calling the method you want to test
执行:调用要测试的方法
Assert: Verifying that your results are correct
断言:验证结果是否正确
Teardown: Cleaning up the modified database records or mocked objects
拆卸:清理修改后的数据库记录或模拟对象
If you want to learn more about test structure, here’s a great read that will show you some best (and worst) practices for writing unit tests:
如果您想了解有关测试结构的更多信息,请阅读以下优秀文章,它将向您展示一些最佳(和最差)的编写单元测试的实践:
Let me take you through a example of what this might look like with a real world step-by-step TDD example.
让我通过一个循序渐进的TDD逐步示例向您展示一个示例。
We’ll make a function that detects a specific mention format in a string and replaces it with the user’s name. The mention format looks like:
我们将创建一个检测字符串中特定提及格式并将其替换为用户名的函数。 提及格式如下:
@(userId)
@(用户身份)
Now that we have a simple case, we will write a test for it. I will use the JavaScript test framework in this example because it’s relatively easy to use:
现在我们有了一个简单的案例,我们将为此编写一个测试。 在此示例中,我将使用JavaScript 测试框架,因为它相对易于使用:
I’ve created a file called parse-mentionable-text.js that returns “OK”.
我创建了一个名为parse-mentionable-text.js的文件,该文件返回“ OK”。
Let me run the test to see if it fails.
让我运行测试以查看是否失败。
Good. When the test fails, we can be sure that the functionality didn’t work when we started.
好。 当测试失败时,我们可以确定开始时该功能不起作用。
This is an important step. In a big code base, you can sometimes write a test for a piece of logic, but because of some side-effect, it will surprise you and actually pass when you expect it to fail. This means that you need to redesign your test. Remember — you have to start with a failing test.
这是重要的一步。 在大型代码库中,有时可以为一段逻辑编写测试,但是由于某些副作用,它会使您感到惊讶,并在期望失败时通过测试。 这意味着您需要重新设计测试。 记住-您必须从失败的测试开始。
After our test has failed, we have to make this test pass with minimal code:
测试失败后,我们必须以最少的代码使该测试通过:
To check whether our implementation meets our requirements, we have to run the test again:
要检查我们的实现是否满足我们的要求,我们必须再次运行测试:
Great! Everything works the way we want it to. Now it’s time to clean up the code and make it prettier and more readable:
大! 一切都按照我们想要的方式进行。 现在是时候清理代码并使其更漂亮,更易读了:
The best part is after you’ve refactored your code, you can check and see whether the function still meets your requirements by running the test again.
最好的部分是在重构代码之后,可以通过再次运行测试来检查并查看该功能是否仍然满足您的要求。
Writing tests first will change the way you code. It will increase your confidence when shipping new code. It will lessen your fear of breaking things, and help you move faster.
首先编写测试将改变您的编码方式。 交付新代码时,它将增加您的信心。 它将减轻您对破碎事物的恐惧,并帮助您更快地移动。
Do you want to learn more about Test Driven Development?
您想要了解更多有关测试驱动开发的信息吗?
If you’re using ReactJS, I’ve also written a post about testing your React components, which you can view .
如果您使用的是ReactJS,我还写了一篇关于测试React组件的文章,您可以查看。
I send out interesting articles about JavaScript & ReactJS every week..
我每周都会发布有关JavaScript和ReactJS的有趣文章。 。
Oh, and click the ? below so other people will see this article here on Medium. Thanks for reading.
哦,然后单击“?”。 下面,其他人会在Medium上看到此文章。 谢谢阅读。
翻译自:
打破双亲委派么,怎么打破
转载地址:http://jeewd.baihongyu.com/