提交 f6f54648 编写于 作者: W wizardforcel

2021-09-26 22:24:41

上级 42aa2673
......@@ -529,7 +529,7 @@ public void whenPlayAndWholeHorizontalLineThenWinner() {
}
```
The player wins when the whole horizontal line is occupied by his pieces.
当整个水平线被他的棋子占据时,玩家获胜。
# 实施
......
......@@ -130,7 +130,7 @@ TestNG 提供了更多的特性,并且比 JUnit 更高级。在本章中,我
想象一下,一艘军舰被安置在地球海洋的某个地方。由于现在是 21 世纪,我们可以远程控制这艘船。
Our job will be to create a program that can move the ship around the seas.
我们的工作将是创建一个可以在海上移动船舶的程序。
因为这是一本 TDD 书籍,本章的主题是单元测试,所以我们将使用 TDD 方法开发一个应用程序,重点是单元测试。在上一章第 3 章*红绿重构——从失败到成功,直到完美*中,您学习了红绿重构过程的理论和实践经验。我们将在此基础上进一步学习如何有效地使用单元测试。具体地说,我们将试着专注于我们正在开发的单元,并学习如何隔离和忽略单元可能使用的依赖项。不仅如此,我们还将努力一次只关注一个需求。出于这个原因,您只收到了高级别的需求;我们应该能够移动位于地球某处的遥控飞船。
......@@ -180,7 +180,7 @@ Our job will be to create a program that can move the ship around the seas.
我们需要知道船的当前位置,以便能够移动它。此外,我们还应该知道它所面临的方向:北、南、东或西。因此,第一个要求如下:
You are given the initial starting point (*x*, *y*) of a ship and the direction (*N*, *S*, *E*, or *W*) it is facing.
您将获得一艘船的初始起点`(x, y)`及其面向的方向(`N``S``E``W`)。
在开始处理这个需求之前,让我们先看看可以使用的帮助器类。`Point`类保存`x``y`坐标。它具有以下构造函数:
......@@ -286,7 +286,7 @@ public class ShipSpec {
现在我们知道我们的船在哪里了,让我们试着移动它。首先,我们应该能够前进和后退。
Implement commands that move the ship forwards and backwards (*f* and *b*).
执行使船向前和向后移动的命令(`f``b`)。
`Location`helper 类已经有了实现此功能的`forward``backward`方法:
......@@ -321,11 +321,11 @@ public void givenEastWhenMoveForwardThenXIncreases() {
当在指定的单元中包含外部代码时,至少在本例中,我们应该考虑到外部代码已经过测试这一事实。我们知道它是有效的,因为每次对代码进行任何更改时,我们都会运行所有测试。
Rerun all the tests every time the implementation code changes. 
每次实现代码更改时重新运行所有测试。
This ensures that there is no unexpected side-effect caused by code changes.
这可确保不会因代码更改而导致意外的副作用。
Every time any part of the implementation code changes, all tests should be run. Ideally, tests are fast to execute and can be run by a developer locally. Once code is submitted to the version control, all tests should be run again to ensure that there was no problem due to code merges. This is especially important when more than one developer is working on the code. CI tools, such as Jenkins, Hudson, Travind, Bamboo, and Go-CD should be used to pull the code from the repository, compile it, and run tests.
每次实现代码的任何部分更改时,都应该运行所有测试。 理想情况下,测试可以快速执行并且可以由开发人员在本地运行。 一旦代码提交给版本控制,所有的测试都应该重新运行,以确保没有由于代码合并引起的问题。 当有多个开发人员在处理代码时,这一点尤其重要。 应该使用 CI 工具,例如 Jenkins、Hudson、Travind、Bamboo 和 Go-CD,从存储库中提取代码、编译它并运行测试。
这种方法的另一个问题是,如果外部代码发生更改,将有更多的规范需要更改。理想情况下,我们应该被迫只更改与要修改的单元直接相关的规范。搜索调用该单元的所有其他位置可能非常耗时且容易出错。
......@@ -383,7 +383,7 @@ public boolean moveBackward() {
只把船来回移动我们走不了多远。我们应该能够通过向左或向右旋转船舶来改变方向。
Implement commands that turn the ship left and right (*l* and *r*).
执行使船左右转动的命令(`l``r`)。
在实现了前面的需求之后,这个需求应该非常容易,因为它可以遵循相同的逻辑。`Location`helper 类已经包含了`turnLeft``turnRight`方法,它们可以精确地执行此需求所要求的内容。我们需要做的就是将它们集成到`Ship`类中。
......@@ -464,9 +464,9 @@ public void whenReceiveCommandsFThenForward() {
我们已经谈到了编写通过规范的最简单的代码的重要性。
Write the simplest code to pass the test. This ensures a cleaner and clearer design and avoids unnecessary features 
编写最简单的代码来通过测试。 这确保了更清晰、更清晰的设计,并避免了不必要的功能
The idea is that the simpler the implementation, the better and easier it is to maintain the product. The idea adheres to the KISS principle. It states that most systems work best if they are kept simple rather than made complex; therefore, simplicity should be a key goal in design and unnecessary complexity should be avoided.
这个想法是实施越简单,维护产品就越好、越容易。 这个想法遵循 KISS 原则。 它指出,大多数系统如果保持简单而不是变得复杂,则效果最佳; 因此,简单应该是设计的一个关键目标,应该避免不必要的复杂性。
这是一个应用此规则的好机会。您可能倾向于编写类似以下内容的代码:
......@@ -535,11 +535,11 @@ public void receiveCommands(String commands) {
如果您试图自己编写规范和实现,并且遵循简单性规则,那么您可能需要多次重构代码以获得最终解决方案。简单性是关键,重构通常是受欢迎的必要条件。重构时,请记住,所有规范必须始终通过。
Refactor only after all the tests have passed. 
只有在所有测试都通过后才能重构。
Benefits: refactoring is safe.
好处:重构是安全的。
If all the implementation code that can be affected has tests and if they are all passing, it is relatively safe to refactor. In most cases, there is no need for new tests; small modifications to existing tests should be enough. The expected outcome of refactoring is to have all the tests passing both before and after the code is modified.
如果所有可能受影响的实现代码都有测试并且都通过了,那么重构相对安全。 在大多数情况下,不需要新的测试; 对现有测试进行小的修改就足够了。 重构的预期结果是让所有测试在修改代码之前和之后都通过。
[此需求的完整来源可在`tdd-java-ch04-ship`存储库的`req04-commands`分支中找到](https://bitbucket.org/vfarcic/tdd-java-ch04-ship/branch/req04-commands)
......@@ -547,7 +547,7 @@ If all the implementation code that can be affected has tests and if they are al
地球和其他任何行星一样,都是一个球体。当地球以地图的形式呈现时,到达一个边缘将我们包裹到另一个边缘;例如,当我们向东移动,到达太平洋最远的点时,我们被包裹在地图的西侧,继续向美国移动。此外,为了使移动更容易,我们可以将地图定义为网格。该网格的长度和高度应表示为*x*轴和*y*轴。该网格应具有最大长度(x)和高度(y)。
Implement wrapping from one edge of the grid to another.
实现从网格的一个边缘到另一个边缘的环绕。
# 规范-行星信息
......@@ -674,7 +674,7 @@ public boolean moveForward() {
尽管地球大部分地区被水覆盖(约 70%),但仍有大陆和岛屿可被视为我们遥控船的障碍。我们应该有办法检测我们的下一步行动是否会遇到这些障碍之一。如果发生这种情况,移动应中止,船舶应停留在当前位置并报告障碍物。
Implement surface detection before each move to a new position. If a command encounters a surface, the ship aborts the move, stays on the current position, and reports the obstacle.
在每次移动到新位置之前实施表面检测。 如果命令遇到水面,船舶将中止移动,停留在当前位置,并报告障碍物。
该需求的规范和实现与我们之前所做的非常相似,我们将留给您。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册