在“Programming Ruby 1.9/2.0”一书中,作者给出了一个网球得分类的例子,该类将通过在实际代码之前编写一些RSpec测试来开发。RSpec示例 - 网球足球课程
笔者给大家介绍4个测试:
it "should start with a score of 0-0"
it "should be 15-0 if the server wins a point"
it "should be 0-15 if the receiver wins a point"
it "should be 15-15 after they both win a point"
然后笔者建议读者应该继续前进,通过编写测试这样完成等级:
it "should be 40-0 after the server wins three points"
it "should be W-L after the server wins four points"
it "should be L-W after the receiver wins four points"
it "should be Deuce after each wins three points"
it "should be A-server after each wins three points and the server gets one more"
(实际TennisScorer类为每位玩家添加分数并以“15-15”格式返回)。
作者是否假设代码将像30-15,15-30,0-30,30-0等分数一样工作100%,只要测试成功执行15-0,0- 15和15-15?换句话说,没有必要明确地测试每个可能的分数? 作者建议40-0测试,这是有道理的,因为40打破0-15-30公约(得分* 15),所以40-0测试足以显示40-30,15-40等将工作以及?
另外,也许我太过于复杂,但在我的测试中添加随机分数100000次并动态比较结果是不是更有意义拥有“随机游戏”? (但是我想我的测试可能容易包含一些错误..?)。 我认为,如果我会为乘法方法编写测试(例如,我会检查1 * 2 = 2并假设一切正常吗?),我认为这将是一条路。