提交 42aa2673 编写于 作者: W wizardforcel

2021-09-26 22:22:11

上级 7a654b7b
......@@ -199,7 +199,7 @@ public void whenSomethingThenResultIsSomethingElse() {
有许多不同的方式来命名测试方法。 我的首选方法是使用 BDD 场景中使用的`given/when/then`语法来命名它们。 `Given`描述(前置)条件,`When`描述动作,`Then`描述预期结果。 如果测试没有先决条件(通常使用`@Before``@BeforeClass`注释设置),则可以跳过`Given`
Do not rely only on comments to provide information about test objectives. Comments do not appear when tests are executed from your favorite IDE, nor do they appear in reports generated by the CI or build tools.
不要仅仅依靠注释来提供有关测试目标的信息。 从您喜欢的 IDE 执行测试时不会出现注释,也不会出现在 CI 或构建工具生成的报告中。
除了编写测试,您还需要运行它们。由于我们使用的是 Gradle,因此可以从命令提示符下运行它们:
......@@ -240,7 +240,7 @@ public class TicTacToeSpec {
}
```
When a piece is placed anywhere outside the *x*-axis, then `RuntimeException` is thrown.
当一块被放置在`x`轴之外的任何地方时,就会抛出`RuntimeException`
在这个测试中,我们定义当调用`ticTacToe.play(5, 2)`方法时,`RuntimeException`是预期的。这是一个很短很容易的测试,让它通过也应该很容易。我们所要做的就是创建`play`方法,并确保当`x`参数小于 1 或大于 3(电路板为 3x3)时,它抛出`RuntimeException`。您应该运行此测试三次。第一次,它应该失败,因为`play`方法不存在。一旦添加,它应该会失败,因为没有抛出`RuntimeException`。第三次,它应该是成功的,因为与此测试对应的代码已完全实现。
......@@ -280,7 +280,7 @@ public void whenYOutsideBoardThenRuntimeException() {
}
```
When a piece is placed anywhere outside the *y*-axis, then `RuntimeException` is thrown.
当一块被放置在`y`轴之外的任何地方时,就会抛出`RuntimeException`
# 实施
......@@ -313,7 +313,7 @@ public void whenOccupiedThenRuntimeException() {
}
```
When a piece is placed on an occupied space, then `RuntimeException` is thrown.
当一块被放置在一个被占用的空间时,就会抛出`RuntimeException`
就这样,;这是我们最后一次测试。一旦实现完成,我们就可以认为第一个需求已经完成。
......@@ -378,7 +378,7 @@ private void setBox(int x, int y) {
现在,是时候研究一下该轮到哪个球员上场了。
There should be a way to find out which player should play next.
应该有办法找出下一个应该玩哪个玩家。
我们可以将此要求分为三个测试:
......@@ -398,11 +398,11 @@ import static org.junit.Assert.*;
现在是编写这些测试和实现的时候了。
Write the test before writing the implementation code .
在编写实现代码之前编写测试。
The benefits of doing this are as follows—it ensures that testable code is written and ensures that every line of code gets tests written for it.
这样做的好处如下——它确保编写了可测试的代码,并确保每一行代码都为它编写了测试。
By writing or modifying the test first, the developer is focused on requirements before starting to work on a code. This is the main difference when compared to writing tests after the implementation is done. An additional benefit is that with tests first, we are avoiding the danger that the tests work as quality checking instead of quality assurance.
通过首先编写或修改测试,开发人员在开始处理代码之前专注于需求。 与在实现完成后编写测试相比,这是主要区别。 另一个好处是,首先进行测试,我们避免了测试作为质量检查而不是质量保证的危险。
# 测试–X 首先播放
......@@ -415,7 +415,7 @@ public void givenFirstTurnWhenNextPlayerThenX() {
}
```
The first turn should be played by Player `X`.
第一回合应该由玩家`X`玩。
这个测试应该是不言自明的。我们期待`nextPlayer`方法返回`X`。如果您尝试运行这个,您将看到代码甚至没有编译。那是因为`nextPlayer`方法根本不存在。我们的任务是编写`nextPlayer`方法并确保它返回正确的值。
......@@ -441,7 +441,7 @@ public void givenLastTurnWasXWhenNextPlayerThenO() {
}
```
If the last turn was played by `X`, then the next turn should be played by `O`.
如果上一回合由`X`打,那么下一回合应该由`O`打。
# 实施
......@@ -471,7 +471,7 @@ public char nextPlayer() {
最后,我们可以检查`X`的回合是否在`O`播放之后。
If the last turn was played by `O`, then the next turn should be played by `X`.
如果上一回合由`O`打,那么下一回合应该由`X`打。
没有什么可以做来完成这个测试,因此,这个测试是无用的,应该被丢弃。如果你写这个测试,你会发现它是一个假阳性。它将在不改变实施的情况下通过;试试看。编写此测试,如果它成功而没有编写任何实现代码,则放弃它。
......@@ -481,7 +481,7 @@ If the last turn was played by `O`, then the next turn should be played by `X`.
是时候按照游戏规则努力赢得比赛了。与前面的代码相比,这一部分的工作变得有点乏味。我们应该检查所有可能获胜的组合,如果其中一个组合成功,则宣布获胜。
A player wins by being the first to connect a line of friendly pieces from one side or corner of the board to the other.
玩家首先将一排友好棋子从棋盘的一侧或角落连接到另一侧,从而获胜。
为了检查一行友好的片段是否连接,我们应该验证水平线、垂直线和对角线。
......@@ -497,7 +497,7 @@ public void whenPlayThenNoWinner() {
}
```
If no winning condition is fulfilled, then there is no winner.
如果不满足获胜条件,则没有获胜者。
# 实施
......@@ -606,7 +606,7 @@ public void whenPlayAndWholeVerticalLineThenWinner() {
}
```
The player wins when the whole vertical line is occupied by his pieces.
当整个垂直线被他的棋子占据时,玩家获胜。
# 实施
......@@ -642,7 +642,7 @@ public void whenPlayAndTopBottomDiagonalLineThenWinner() {
}
```
The player wins when the whole diagonal line from the top-left to bottom-right is occupied by his pieces.
当从左上角到右下角的整条对角线被他的棋子占据时,玩家获胜。
# 实施
......@@ -707,7 +707,7 @@ public void whenPlayAndBottomTopDiagonalLineThenWinner() {
}
```
The player wins when the whole diagonal line from the bottom-left to top-right is occupied by his pieces.
当从左下角到右上角的整个对角线都被他的棋子占据时,玩家获胜。
# 实施
......@@ -765,7 +765,7 @@ private boolean isWin() {
唯一缺少的是如何处理平局结果。
The result is a draw when all the boxes are filled.
当所有方框都填满时,结果是平局。
# 测试–处理平局情况
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册