博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
出色的RSpec-tations:为什么我喜欢测试驱动的开发
阅读量:2536 次
发布时间:2019-05-11

本文共 4977 字,大约阅读时间需要 16 分钟。

by Arit Amana

通过阿里特·阿马纳(Arit Amana)

出色的RSpec-tations:为什么我喜欢测试驱动的开发 (Great RSpec-tations: why I love test-driven development)

When I first , I thought I was in love with the concept… but that was just the flirting stage. Now I’ve fallen head-over-Louboutins, girlfriend! ?

当我第一次 ,我以为我爱上了这个概念……但这只是调情的阶段。 现在我跌倒了Louboutins,女朋友! ?

The efficiency gains from not needing to fire my app up for testing are just the beginning. Developing my apps with forces me to really think about how I’m defining and structuring my code. Furthermore, every time my tests fail, they faithfully supply tips and clues that help me troubleshoot what’s missing, broken or redundant. Now that’s a metaphor for life in general… but I digress. ?

无需启动我的应用程序进行测试就可以提高效率,这仅仅是开始。 使用开发我的应用程序迫使我真正考虑如何定义和构建代码。 此外,每当我的测试失败时,它们就会忠实地提供提示和线索,以帮助我解决丢失,损坏或多余的内容。 现在,这是对一般生活的隐喻……但是我离题了。 ?

I’m creating an online chess app , and this week, I was tasked with building the move_to!(x,y) method. This should move a chess piece (called pawn from now on) to the chessboard square at location (x,y).

我正在创建一个在线国际象棋应用程序,本周,我的任务是构建move_to!(x,y)方法。 这应该将棋子(从现在开始称为棋子 )移动到位置(x,y)的棋盘正方形。

If an opponent’s piece (called king from now on) occupies (x,y), pawn should capture it. If pawn’s brother-in-arms occupies (x,y), the method should raise an error message and pawn should go nowhere.

如果对手的棋子(从现在起称为国王 )占据了(x,y),则典当应该将其捕获。 如果pawn的兄弟关系占用(x,y),则该方法应引发错误消息,并且pawn不应到达任何地方。

Note: move_to!(x,y) doesn’t consider whether the moves or captures are valid. Other methods will do this.

注意:move_to!(x,y)不会考虑移动或捕获是否有效。 其他方法可以做到这一点。

I configured to generate instances of a chess game. Each chess piece has the following relevant attributes: :location_x, :location_y, :white (a boolean; true = white color), :game_id, and :notcaptured (a boolean; false = the piece has been captured). My first test determined if pawn (currently on 0,0) moved to an empty square (7,7):

我将配置为生成国际象棋游戏的实例。 每个棋子都有以下相关属性:: location_x:location_y:white (一个布尔值; true =白色) ,: game_id:notcaptured (一个布尔值; false =已捕获该棋子)。 我的第一个测试确定了pawn(当前在0,0上)是否移到一个空的正方形(7,7):

Next, I began writing the move_to!(x,y) method, then ran my test:

接下来,我开始编写move_to!(x,y)方法,然后运行测试:

arit (master) chessapp $ rspec spec/models/piece_spec.rb
.
Finished in 0.43495 seconds (files took 15.68 seconds to load)
1 example, 0 failures

Yes! No errors. ?? Next, I wrote a test to determine whether pawn stayed put if its destination was occupied by a friendly piece (we’ll call it rook):

是! 没有错误。 ?? 接下来,我编写了一个测试来确定当棋子的目的地是否被友善棋子占据时,pawn是否保持原状(我们称其为ok):

Why aren’t I testing the values of rook.notcaptured, rook.location_x and rook.location_y? Well, the rook IS the friendly-piece in question, but what we’re actually testing is whatever piece (if any) is found by and saved in the destination variable. Now to flesh out the method:

我为什么不测试rook.notcapturedrook.location_xrook.location_y的值? 好吧,rook是有问题的友好部件,但是我们实际测试的是目标变量找到并保存在目标变量中的任何部件(如果有)。 现在来充实该方法:

My tests passed again! ?? Feeling very confident, I moved on to the third test: to determine if the opponent’s king was captured and whether pawn took its place:

我的测试又通过了! ?? 感到非常自信,我进行了第三项测试:确定是否捕获了对手的国王,以及是否使用了当兵:

I also completed the method:

我还完成了该方法:

But when I ran my tests, I received the following error:

但是,当我运行测试时,收到以下错误:

arit (master *) chessapp $ rspec spec/models/piece_spec.rb
..F
Failures:
1) Piece captures opponent's piece on destination, then assumes that position
Failure/Error: expect(destination.notcaptured).to be false
expected false
got true
# ./spec/models/piece_spec.rb:104:in `block (2 levels) in 
'
Finished in 0.21787 seconds (files took 4.83 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/piece_spec.rb:95 # Piece captures opponent's piece on destination, then assumes that position

Wha??? destination.notcaptured was not updated? Why? I re-read my method over and over. Nothing seemed to be missing or broken (and, really, just how much could I get wrong in 11 lines of code?).

哇??? destination.notcaptured是否未更新? 为什么? 我一遍又一遍地重新阅读我的方法。 似乎没有什么丢失或损坏的东西(实际上,在11行代码中到底有多少我会出错?)。

After deciding to make like a ? and review my rspec test sloooowwly, it occurred to me that the destination variable was expected to change. The move_to!(x,y) method had updated its location_x, location_y and notcaptured attributes.

决定做出像? 然后仔细检查我的rspec测试,发现目标变量预期会发生变化。 move_to!(x,y)方法已更新其location_x,location_yotcaptured属性。

Then it hit me — I needed to RELOAD destination from the database back into RSpec. Then my three tests passed beautifully:

然后让我震惊了—我需要将数据库中的目标重新加载回RSpec。 然后,我的三项测试顺利通过:

Test-Driven Development has permanently impacted my coding practice, and I relished the opportunity to turn the rest of my teammates unto it. TDD is lightweight, efficient, safe, revealing, and it helps me produce higher-quality code the first … well, okay… in as little time as possible! ?

测试驱动开发对我的编码实践产生了永久性的影响,因此我很高兴有机会将我的其他队友变成现实。 TDD是轻量级的,高效的,安全的,可显示的,它可以帮助我在尽可能短的时间内首先生成高质量的代码……好吧,好吧! ?

翻译自:

转载地址:http://wikzd.baihongyu.com/

你可能感兴趣的文章
使用Android OpenGL ES 2.0绘图之六:响应触摸事件
查看>>
我们过去几年做对了哪些事
查看>>
ubuntu 16.04LTS
查看>>
javascript深入理解js闭包
查看>>
Oracle的安装
查看>>
Android Socket连接PC出错问题及解决
查看>>
Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
查看>>
第2天线性表链式存储
查看>>
python自动化测试-D11-学习笔记之一(yaml文件,ddt)
查看>>
mysql存储过程使用游标循环插入数据
查看>>
Ubuntu 12.04 添加新用户并启用root登录
查看>>
20145309信息安全系统设计基础第9周学习总结上
查看>>
c# 字段、属性get set
查看>>
td内容超出隐藏
查看>>
Spring CommonsMultipartResolver 上传文件
查看>>
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>